Ejemplo n.º 1
0
def folder_key_metadata_object():
    content = OrderedDict(Key='naptime/',
                          LastModified='2009-10-12T17:50:30.000Z',
                          ETag='"fba9dede5f27731c9771645a39863328"',
                          Size='0',
                          StorageClass='STANDARD')

    return S3FolderKeyMetadata(content)
Ejemplo n.º 2
0
    async def _metadata_folder(self, path):
        await self._check_region()

        params = {'prefix': path.path, 'delimiter': '/'}
        resp = await self.make_request(
            'GET',
            functools.partial(self.bucket.generate_url,
                              settings.TEMP_URL_SECS,
                              'GET',
                              query_parameters=params),
            params=params,
            expects=(200, ),
            throws=exceptions.MetadataError,
        )

        contents = await resp.read()

        parsed = xmltodict.parse(contents,
                                 strip_whitespace=False)['ListBucketResult']

        contents = parsed.get('Contents', [])
        prefixes = parsed.get('CommonPrefixes', [])

        if not contents and not prefixes and not path.is_root:
            # If contents and prefixes are empty then this "folder"
            # must exist as a key with a / at the end of the name
            # if the path is root there is no need to test if it exists
            resp = await self.make_request(
                'HEAD',
                functools.partial(
                    self.bucket.new_key(path.path).generate_url,
                    settings.TEMP_URL_SECS, 'HEAD'),
                expects=(200, ),
                throws=exceptions.MetadataError,
            )
            await resp.release()

        if isinstance(contents, dict):
            contents = [contents]

        if isinstance(prefixes, dict):
            prefixes = [prefixes]

        items = [S3FolderMetadata(item) for item in prefixes]

        for content in contents:
            if content['Key'] == path.path:
                continue

            if content['Key'].endswith('/'):
                items.append(S3FolderKeyMetadata(content))
            else:
                items.append(S3FileMetadata(content))

        return items