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
def create_folder(self, path, **kwargs): """ :param str path: The path to create a folder at """ WaterButlerPath.validate_folder(path) if (yield from self.exists(path)): raise exceptions.FolderNamingConflict(str(path)) yield from self.make_request( 'PUT', self.bucket.new_key(path.path).generate_url(settings.TEMP_URL_SECS, 'PUT'), expects=(200, 201), throws=exceptions.CreateFolderError ) return S3FolderMetadata({'Prefix': path.path})
async def create_folder(self, path, folder_precheck=True, **kwargs): """ :param str path: The path to create a folder at """ await self._check_region() WaterButlerPath.validate_folder(path) if folder_precheck: if (await self.exists(path)): raise exceptions.FolderNamingConflict(str(path)) async with self.request( 'PUT', functools.partial(self.bucket.new_key(path.path).generate_url, settings.TEMP_URL_SECS, 'PUT'), skip_auto_headers={'CONTENT-TYPE'}, expects=(200, 201), throws=exceptions.CreateFolderError ): return S3FolderMetadata({'Prefix': path.path})
def folder_metadata_object(): content = OrderedDict(Prefix='photos/') return S3FolderMetadata(content)