Package: aiohttp-s3-client
aiohttp-s3-clientThe simple module for putting and getting object from Amazon S3 compatible endpointsInstallationpipinstallaiohttp-s3-clientUsagefromhttpimportHTTPStatusfromaiohttpimportClientSessionfromaiohttp_s3_clientimportS3ClientasyncwithClientSession(raise_for_status=True)assession:client=S3Client(url="http://s3-url",session=session,access_key_id="key-id",secret_access_key="hackme",region="us-east-1")# Upload str object to bucket "bucket" and key "str"asyncwithclient.put("bucket/str","hello, world")asresp:assertresp.status==HTTPStatus.OK# Upload bytes object to bucket "bucket" and key "bytes"asyncwithawaitclient.put("bucket/bytes",b"hello, world")asresp:assertresp.status==HTTPStatus.OK# Upload AsyncIterable to bucket "bucket" and key "iterable"asyncdefgen():yieldb'some bytes'asyncwithclient.put("bucket/file",gen())asresp:assertresp.status==HTTPStatus.OK# Upload file to bucket "bucket" and key "file"asyncwithclient.put_file("bucket/file","/path_to_file")asresp:assertresp.status==HTTPStatus.OK# Check object exists using bucket+keyasyncwithclient.head("bucket/key")asresp:assertresp==HTTPStatus.OK# Get object by bucket+keyasyncwithclient.get("bucket/key")asresp:data=awaitresp.read()# Delete object using bucket+keyasyncwithclient.delete("bucket/key")asresp:assertresp==HTTPStatus.NO_CONTENT# List objects by prefixasyncforresultinclient.list_objects_v2("bucket/",prefix="prefix"):# Each result is a list of metadata objects representing an object# stored in the bucket.do_work(result)Bucket may be specified as subdomain or in object name:importaiohttpfromaiohttp_s3_clientimportS3Clientclient=S3Client(url="http://bucket.your-s3-host",session=aiohttp.ClientSession())asyncwithclient.put("key",gen())asresp:...client=S3Client(url="http://your-s3-host",session=aiohttp.ClientSession())asyncwithawaitclient.put("bucket/key",gen())asresp:...client=S3Client(url="http://your-s3-host/bucket",session=aiohttp.ClientSession())asyncwithclient.put("key",gen())asresp:...Auth may be specified with keywords or in URL:importaiohttpfromaiohttp_s3_clientimportS3Clientclient_credentials_as_kw=S3Client(url="http://your-s3-host",access_key_id="key_id",secret_access_key="access_key",session=aiohttp.ClientSession(),)client_credentials_in_url=S3Client(url="http://key_id:access_key@your-s3-host",session=aiohttp.ClientSession(),)CredentialsBy defaultS3Clienttrying to collect all available credentials from keyword
arguments likeaccess_key_id=andsecret_access_key=, after that from the
username and password from passedurlargument, so the next step is environment
variables parsing and the last source for collection is the config file.You can pass credentials explicitly usingaiohttp_s3_client.credentialsmodule.aiohttp_s3_client.credentials.StaticCredentialsimportaiohttpfromaiohttp_s3_clientimportS3Clientfromaiohttp_s3_client.credentialsimportStaticCredentialscredentials=StaticCredentials(access_key_id='aaaa',secret_access_key='bbbb',region='us-east-1',)client=S3Client(url="http://your-s3-host",session=aiohttp.ClientSession(),credentials=credentials,)aiohttp_s3_client.credentials.URLCredentialsimportaiohttpfromaiohttp_s3_clientimportS3Clientfromaiohttp_s3_client.credentialsimportURLCredentialsurl="http://key@hack-me:your-s3-host"credentials=URLCredentials(url,region="us-east-1")client=S3Client(url="http://your-s3-host",session=aiohttp.ClientSession(),credentials=credentials,)aiohttp_s3_client.credentials.EnvironmentCredentialsimportaiohttpfromaiohttp_s3_clientimportS3Clientfromaiohttp_s3_client.credentialsimportEnvironmentCredentialscredentials=EnvironmentCredentials(region="us-east-1")client=S3Client(url="http://your-s3-host",session=aiohttp.ClientSession(),credentials=credentials,)aiohttp_s3_client.credentials.ConfigCredentialsUsing user config file:importaiohttpfromaiohttp_s3_clientimportS3Clientfromaiohttp_s3_client.credentialsimportConfigCredentialscredentials=ConfigCredentials()# Will be used ~/.aws/credentials configclient=S3Client(url="http://your-s3-host",session=aiohttp.ClientSession(),credentials=credentials,)Using the custom config location:importaiohttpfromaiohttp_s3_clientimportS3Clientfromaiohttp_s3_client.credentialsimportConfigCredentialscredentials=ConfigCredentials("~/.my-custom-aws-credentials")client=S3Client(url="http://your-s3-host",session=aiohttp.ClientSession(),credentials=credentials,)aiohttp_s3_client.credentials.merge_credentialsThis function collect all passed credentials instances and return a new one
which contains all non-blank fields from passed instances. The first argument
has more priority.importaiohttpfromaiohttp_s3_clientimportS3Clientfromaiohttp_s3_client.credentialsimport(ConfigCredentials,EnvironmentCredentials,merge_credentials)credentials=merge_credentials(EnvironmentCredentials(),ConfigCredentials(),)client=S3Client(url="http://your-s3-host",session=aiohttp.ClientSession(),credentials=credentials,)aiohttp_s3_client.credentials.MetadataCredentialsTrying to get credentials from the metadata service:importaiohttpfromaiohttp_s3_clientimportS3Clientfromaiohttp_s3_client.credentialsimportMetadataCredentialscredentials=MetadataCredentials()# start refresh credentials from metadata serverawaitcredentials.start()client=S3Client(url="http://your-s3-host",session=aiohttp.ClientSession(),)awaitcredentials.stop()Multipart uploadFor uploading large filesmultipart uploadingcan be used. It allows you to asynchronously upload multiple parts of a file
to S3.
S3Client handles retries of part uploads and calculates part hash for integrity checks.importaiohttpfromaiohttp_s3_clientimportS3Clientclient=S3Client(url="http://your-s3-host",session=aiohttp.ClientSession())awaitclient.put_file_multipart("test/bigfile.csv",headers={"Content-Type":"text/csv",},workers_count=8,)Parallel download to fileS3 supportsGETrequests withRangeheader. It's possible to download
objects in parallel with multiple connections for speedup.
S3Client handles retries of partial requests and makes sure that file won't
be changed during download withETagheader.
If your system supportspwritesyscall (Linux, macOS, etc.) it will be used to
write simultaneously to a single file. Otherwise, each worker will have own file
which will be concatenated after downloading.importaiohttpfromaiohttp_s3_clientimportS3Clientclient=S3Client(url="http://your-s3-host",session=aiohttp.ClientSession())awaitclient.get_file_parallel("dump/bigfile.csv","/home/user/bigfile.csv",workers_count=8,)