Parse a single S3 bucket

        TODO:
        - CORS
        - Lifecycle
        - Notification ?
        - Get bucket's policy

        :param bucket:
        :param params:
        :return:
326616
def parse_buckets(self, bucket, params):
        
        bucket['name'] = bucket.pop('Name')
        api_client = params['api_clients'][get_s3_list_region(list(params['api_clients'].keys())[0])]

        bucket['CreationDate'] = str(bucket['CreationDate'])
        bucket['region'] = get_s3_bucket_location(api_client, bucket['name'])
        # h4ck :: fix issue #59, location constraint can be EU or eu-west-1 for Ireland...
        if bucket['region'] == 'EU':
            bucket['region'] = 'eu-west-1'
        # h4ck :: S3 is global but region-aware...
        if bucket['region'] not in params['api_clients']:
            printInfo('Skipping bucket %s (region %s outside of scope)' % (bucket['name'], bucket['region']))
            self.buckets_count -= 1
            return

        api_client = params['api_clients'][bucket['region']]
        get_s3_bucket_logging(api_client, bucket['name'], bucket)
        get_s3_bucket_versioning(api_client, bucket['name'], bucket)
        get_s3_bucket_webhosting(api_client, bucket['name'], bucket)
        get_s3_bucket_default_encryption(api_client, bucket['name'], bucket)
        bucket['grantees'] = get_s3_acls(api_client, bucket['name'], bucket)
        get_s3_bucket_policy(api_client, bucket['name'], bucket)
        get_s3_bucket_secure_transport(api_client, bucket['name'], bucket)
        # If requested, get key properties
        #if params['check_encryption'] or params['check_acls']:
        #    get_s3_bucket_keys(api_client, bucket['name'], bucket, params['check_encryption'],
        #                       params['check_acls'])
        bucket['id'] = self.get_non_aws_id(bucket['name'])
        self.buckets[bucket['id']] = bucket
1313715
def get_bucket_policy(self, bucket_name):
        
        is_valid_bucket_name(bucket_name)

        response = self._url_open("GET",
                                  bucket_name=bucket_name,
                                  query={"policy": ""})
        return response.data
1028275
def decode(cls, key):
        

        # Determine bucket key version
        prefix, sep, param_str = key.partition(':')
        if sep != ':' or prefix not in cls._prefix_to_version:
            raise ValueError("%r is not a bucket key" % key)
        version = cls._prefix_to_version[prefix]

        # Take the parameters apart...
        parts = param_str.split('/')
        uuid = parts.pop(0)
        params = {}
        for part in parts:
            name, sep, value = part.partition('=')

            # Make sure it's well-formed
            if sep != '=':
                raise ValueError("Cannot interpret parameter expression %r" %
                                 part)

            params[name] = cls._decode(value)

        # Return a BucketKey
        return cls(uuid, params, version=version)
906468
function buildLogBucketPolicy() {
  return {
    LogBucketPolicy: {
      Type: 'AWS::S3::BucketPolicy',
      Properties: {
        Bucket: {
          Ref: 'LogBucket',
        },
        PolicyDocument: {
          Version: '2012-10-17',
          Statement: [
            {
              Sid: 'AWSLogDeliveryAclCheck',
              Effect: 'Allow',
              Principal: {
                Service: 'delivery.logs.amazonaws.com',
              },
              Action: 's3:GetBucketAcl',
              Resource: {
                'Fn::GetAtt': ['LogBucket', 'Arn'],
              },
            },
            {
              Sid: 'AWSLogDeliveryWrite',
              Effect: 'Allow',
              Principal: {
                Service: 'delivery.logs.amazonaws.com',
              },
              Action: 's3:PutObject',
              Resource: {
                'Fn::Join': [
                  '',
                  [
                    'arn:aws:s3:::',
                    { Ref: 'LogBucket' },
                    '/AWSLogs/',
                    { Ref: 'AWS::AccountId' },
                    '/*',
                  ],
                ],
              },
              Condition: {
                StringEquals: {
                  's3:x-amz-acl': 'bucket-owner-full-control',
                },
              },
            },
          ],
        },
      },
    },
  };
}
376986
def _construct_body_s3_dict(self):
        
        if isinstance(self.definition_uri, dict):
            if not self.definition_uri.get("Bucket", None) or not self.definition_uri.get("Key", None):
                # DefinitionUri is a dictionary but does not contain Bucket or Key property
                raise InvalidResourceException(self.logical_id,
                                               "'DefinitionUri' requires Bucket and Key properties to be specified")
            s3_pointer = self.definition_uri

        else:

            # DefinitionUri is a string
            s3_pointer = parse_s3_uri(self.definition_uri)
            if s3_pointer is None:
                raise InvalidResourceException(self.logical_id,
                                               '\'DefinitionUri\' is not a valid S3 Uri of the form '
                                               '"s3://bucket/key" with optional versionId query parameter.')

        body_s3 = {
            'Bucket': s3_pointer['Bucket'],
            'Key': s3_pointer['Key']
        }
        if 'Version' in s3_pointer:
            body_s3['Version'] = s3_pointer['Version']
        return body_s3
412781
def describe(Bucket,
             region=None, key=None, keyid=None, profile=None):
    '''
    

    '''

    try:
        conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
        result = {}
        conn_dict = {'ACL': conn.get_bucket_acl,
                     'CORS': conn.get_bucket_cors,
                     'LifecycleConfiguration': conn.get_bucket_lifecycle_configuration,
                     'Location': conn.get_bucket_location,
                     'Logging': conn.get_bucket_logging,
                     'NotificationConfiguration': conn.get_bucket_notification_configuration,
                     'Policy': conn.get_bucket_policy,
                     'Replication': conn.get_bucket_replication,
                     'RequestPayment': conn.get_bucket_request_payment,
                     'Versioning': conn.get_bucket_versioning,
                     'Website': conn.get_bucket_website}

        for key, query in six.iteritems(conn_dict):
            try:
                data = query(Bucket=Bucket)
            except ClientError as e:
                if e.response.get('Error', {}).get('Code') in (
                            'NoSuchLifecycleConfiguration',
                            'NoSuchCORSConfiguration',
                            'NoSuchBucketPolicy',
                            'NoSuchWebsiteConfiguration',
                            'ReplicationConfigurationNotFoundError',
                            'NoSuchTagSet',
                            ):
                    continue
                raise
            if 'ResponseMetadata' in data:
                del data['ResponseMetadata']
            result[key] = data

        tags = {}
        try:
            data = conn.get_bucket_tagging(Bucket=Bucket)
            for tagdef in data.get('TagSet'):
                tags[tagdef.get('Key')] = tagdef.get('Value')
        except ClientError as e:
            if not e.response.get('Error', {}).get('Code') == 'NoSuchTagSet':
                raise
        if tags:
            result['Tagging'] = tags
        return {'bucket': result}
    except ClientError as e:
        err = __utils__['boto3.get_error'](e)
        if e.response.get('Error', {}).get('Code') == 'NoSuchBucket':
            return {'bucket': None}
        return {'error': __utils__['boto3.get_error'](e)}
367056
function configurePolicyForBucket(aws, bucketName, customPolicy) {
  const policy = customPolicy || {
    Version: '2012-10-17',
    Statement: [
      {
        Effect: 'Allow',
        Principal: {
          AWS: '*'
        },
        Action: 's3:GetObject',
        Resource: `arn:aws:s3:::${bucketName}/*`
      }
    ]
  };

  const params = {
    Bucket: bucketName,
    Policy: JSON.stringify(policy)
  };

  return aws.request('S3', 'putBucketPolicy', params);
}
205446
def get_bucket_versioning_config(self, bucket):
        
        details = self._details(
            method=b"GET",
            url_context=self._url_context(bucket=bucket, object_name="?versioning"),
        )
        d = self._submit(self._query_factory(details))
        d.addCallback(self._parse_versioning_config)
        return d
774406
def get(self, url=None, delimiter="/"):
        
        params = {'Delimiter': delimiter}
        bucket, obj_key = _parse_url(url)

        if bucket:
            params['Bucket'] = bucket
        else:
            return self.call("ListBuckets", response_data_key="Buckets")

        if obj_key:
            params['Prefix'] = obj_key

        objects = self.call("ListObjects", response_data_key="Contents",
                            **params)
        if objects:
            for obj in objects:
                obj['url'] = "s3://{0}/{1}".format(bucket, obj['Key'])

        return objects
203968
def getkeys(self, path, filename=None, directories=False, recursive=False):
        
        from .utils import connection_with_anon, connection_with_gs

        parse = BotoClient.parse_query(path)

        scheme = parse[0]
        bucket_name = parse[1]
        key = parse[2]

        if scheme == 's3' or scheme == 's3n':
            conn = connection_with_anon(self.credentials)
            bucket = conn.get_bucket(bucket_name)
        elif scheme == 'gs':
            conn = connection_with_gs(bucket_name)
            bucket = conn.get_bucket()
        else:
            raise NotImplementedError("No file reader implementation for URL scheme " + scheme)

        if filename:
            if not key.endswith("/"):
                if self.check_prefix(bucket, key + "/"):
                    key += "/"
                else:
                    index = key.rfind("/")
                    if index >= 0:
                        key = key[:(index+1)]
                    else:
                        key = ""
            key += filename

        keylist = BotoClient.retrieve_keys(bucket, key, prefix=parse[3], postfix=parse[4],
                                           directories=directories, recursive=recursive)
        return scheme, keylist
761587
def parse_queue(self, global_params, region, queue_url):
        
        queue = {'QueueUrl': queue_url}
        attributes = api_clients[region].get_queue_attributes(QueueUrl = queue_url, AttributeNames = ['CreatedTimestamp', 'Policy', 'QueueArn'])['Attributes']
        queue['arn'] = attributes.pop('QueueArn')
        for k in ['CreatedTimestamp']:
            queue[k] = attributes[k] if k in attributes else None
        if 'Policy' in attributes:
            queue['Policy'] = json.loads(attributes['Policy'])
        else:
            queue['Policy'] = {'Statement': []}

        queue['name'] = queue['arn'].split(':')[-1]
        self.queues[queue['name']] = queue
818153
def download_object(self, instance, bucket_name, object_name):
        
        url = '/buckets/{}/{}/{}'.format(instance, bucket_name, object_name)
        response = self._client.get_proto(path=url)
        return response.content
453607
def bucket(options={})
      defaults = {:bucket => nil, :acl => :private, :location_constraint => 'us-east-1', :create_if_missing => false}
      options = parse_opts(options, defaults)

      s3b = @s3.buckets[options[:bucket]]
      return s3b if s3b.exists?
      return @s3.buckets.create(options[:bucket],
                         :acl => options[:acl],
                         :location_constraint => options[:location_constraint]) if options[:create_if_missing]
      nil
    end
1371052
def process_bucket_inventory(bid, inventory_bucket, inventory_prefix):
    
    log.info("Loading bucket %s keys from inventory s3://%s/%s",
             bid, inventory_bucket, inventory_prefix)
    account, bucket = bid.split(':', 1)
    region = connection.hget('bucket-regions', bid)
    versioned = bool(int(connection.hget('bucket-versions', bid)))
    session = boto3.Session()
    s3 = session.client('s3', region_name=region, config=s3config)

    # find any key visitors with inventory filtering
    account_info = json.loads(connection.hget('bucket-accounts', account))
    ifilters = [v.inventory_filter for v
                in get_key_visitors(account_info) if v.inventory_filter]

    with bucket_ops(bid, 'inventory'):
        page_iterator = load_bucket_inventory(
            s3, inventory_bucket, inventory_prefix, versioned, ifilters)
        if page_iterator is None:
            log.info("bucket:%s could not find inventory" % bid)
            # case: inventory configured but not delivered yet
            # action: dispatch to bucket partition (assumes 100k+ for inventory)
            # - todo consider max inventory age/staleness for usage
            return invoke(process_bucket_partitions, bid)
        connection.hset('buckets-inventory', bid, 1)
        for page in page_iterator:
            invoke(process_keyset, bid, page)
981599
def parse_get_bucket_notification(data):
    
    root = S3Element.fromstring('GetBucketNotificationResult', data)

    notifications = _parse_add_notifying_service_config(
        root, {},
        'TopicConfigurations', 'TopicConfiguration'
    )
    notifications = _parse_add_notifying_service_config(
        root, notifications,
        'QueueConfigurations', 'QueueConfiguration'
    )
    notifications = _parse_add_notifying_service_config(
        root, notifications,
        'CloudFunctionConfigurations', 'CloudFunctionConfiguration'
    )

    return notifications
546105
def process_bucket_set(account_info, buckets):
    
    region_clients = {}
    log = logging.getLogger('salactus.bucket-set')
    log.info("processing account %s", account_info)
    session = get_session(account_info)
    client = session.client('s3', config=s3config)

    for b in buckets:
        bid = bucket_id(account_info, b)
        with bucket_ops(bid):
            info = {'name': b}
            error = None

            try:
                location = client.get_bucket_location(
                    Bucket=b).get('LocationConstraint')
            except Exception as e:
                error = e
                location = None

            if location is None:
                region = "us-east-1"
            elif location == 'EU':
                region = "eu-west-1"
            else:
                region = location

            if (account_info.get('regions', ()) and
                    region not in account_info.get('regions', ())):
                continue

            info['region'] = region
            if region not in region_clients:
                region_clients.setdefault(region, {})
                region_clients[region]['s3'] = s3 = session.client(
                    's3', region_name=region, config=s3config)
                region_clients[region]['cloudwatch'] = cw = session.client(
                    'cloudwatch', region_name=region, config=s3config)
            else:
                s3 = region_clients[region]['s3']
                cw = region_clients[region]['cloudwatch']

            try:
                info['keycount'] = bucket_key_count(cw, info)
            except Exception:
                raise
            else:
                connection.hset('bucket-sizes', bid, info['keycount'])

            if error:
                raise error

            connection.hset('bucket-regions', bid, region)

            versioning = s3.get_bucket_versioning(Bucket=b)
            info['versioned'] = (
                versioning and versioning.get('Status', '')
                in ('Enabled', 'Suspended') or False)
            connection.hset('bucket-versions', bid, int(info['versioned']))

            log.info("processing bucket %s", info)
            connection.hset('bucket-starts', bid, time.time())
            dispatch_object_source(s3, account_info, bid, info)
1145054
function(params, callback) {
        var err = null;

        if (typeof(params) === "object" && params !== null)
        {
            if(typeof(params.Bucket) !== "string" || params.Bucket.length <= 0)
            {
                err = new Error("Mock-AWS-S3: Argument 'params' must contain a 'Bucket' (String) property");
            }
        }
        else {
            err = new Error("Mock-AWS-S3: Argument 'params' must be an Object");
        }

        var opts = _.extend({}, this.defaultOptions, applyBasePath(params));

        if (err !== null) {
            callback(err);
        }

        var bucketPath = opts.basePath || "";
        bucketPath += opts.Bucket;

        fs.remove(bucketPath, function(err) {
            return callback(err);
        });
	}