Пример #1
0
    def list(self, request):
        r"""Retrieves a list of objects matching the criteria.

    Args:
      request: (ListRequest) input message
    Returns:
      (ListResponse) The response message.
    """
        kwargs = {'Bucket': request.bucket, 'Prefix': request.prefix}

        if request.continuation_token is not None:
            kwargs['ContinuationToken'] = request.continuation_token

        try:
            boto_response = self.client.list_objects_v2(**kwargs)
        except Exception as e:
            message = e.response['Error']['Message']
            code = e.response['ResponseMetadata']['HTTPStatusCode']
            raise messages.S3ClientError(message, code)

        if boto_response['KeyCount'] == 0:
            message = 'Tried to list nonexistent S3 path: s3://%s/%s' % (
                request.bucket, request.prefix)
            raise messages.S3ClientError(message, 404)

        items = [
            messages.Item(etag=content['ETag'],
                          key=content['Key'],
                          last_modified=content['LastModified'],
                          size=content['Size'])
            for content in boto_response['Contents']
        ]

        try:
            next_token = boto_response['NextContinuationToken']
        except KeyError:
            next_token = None

        response = messages.ListResponse(items, next_token)
        return response
Пример #2
0
    def list(self, request):
        bucket = request.bucket
        prefix = request.prefix or ''
        matching_files = []

        for file_bucket, file_name in sorted(iter(self.files)):
            if bucket == file_bucket and file_name.startswith(prefix):
                file_object = self.get_file(file_bucket,
                                            file_name).get_metadata()
                matching_files.append(file_object)

        if not matching_files:
            message = 'Tried to list nonexistent S3 path: s3://%s/%s' % (
                bucket, prefix)
            raise messages.S3ClientError(message, 404)

        # Handle pagination.
        items_per_page = 5
        if not request.continuation_token:
            range_start = 0
        else:
            if request.continuation_token not in self.list_continuation_tokens:
                raise ValueError('Invalid page token.')
            range_start = self.list_continuation_tokens[
                request.continuation_token]
            del self.list_continuation_tokens[request.continuation_token]

        result = messages.ListResponse(
            items=matching_files[range_start:range_start + items_per_page])

        if range_start + items_per_page < len(matching_files):
            next_range_start = range_start + items_per_page
            next_continuation_token = '_page_token_%s_%s_%d' % (
                bucket, prefix, next_range_start)
            self.list_continuation_tokens[
                next_continuation_token] = next_range_start
            result.next_token = next_continuation_token

        return result