Import AWS SDK for Python

Ensure that Python v3.7 or a higher version is installed.

Initialize a venv project

python3 -m venv .venv
. .venv/bin/activate

Add AWS SDK for Python

python3 -m pip install boto3==1.28.85

For each code example that follows,create the code under the main.py ,then execute

python3 main.py

UploadObject

Client-side upload

Create main.py

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
presigned_url = s3.generate_presigned_url(ClientMethod='put_object', Params={'Bucket': '<Bucket>', 'Key': '<Key>'})
print(presigned_url)

This code will generate a pre-signed client-side upload URL, valid for 1 hours, which the client can use to send an PUT request and upload a file within the expiration time.

The following is an example of uploading a file using curl:

curl -X PUT --upload-file "<path/to/file>" "<presigned url>"

You can also specify the expiration time for the upload credentials, for example:

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
presigned_url = s3.generate_presigned_url(ClientMethod='put_object', Params={'Bucket': '<Bucket>', 'Key': '<Key>'}, ExpiresIn=86400)
print(presigned_url)

Server-side upload

PutObject(file)

Create main.py

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
with open('<path/to/upload>', 'rb') as f:
    s3.put_object(Bucket='<Bucket>', Key='<Key>', Body=f)

PutObject(stream)

Create main.py

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
s3.put_object(Bucket='<Bucket>', Key='<Key>', Body='Hello, SUFY S3!')

MultipartUpload(file)

Create main.py

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
create_multipart_upload_response = s3.create_multipart_upload(Bucket='<Bucket>', Key='<Key>')
uploaded = 0
PART_SIZE = 5*1024*1024 # part size is 5 MB
part_number = 1
parts = []
with open('<path/to/upload>', 'rb') as f:
    # The example given here is a serial multipart upload. You can modify it to perform a parallel multipart upload to further improve the upload speed.
    while True:
        part_body = f.read(PART_SIZE)
        if len(part_body) == 0:
            break
        upload_part_response = s3.upload_part(Bucket=create_multipart_upload_response['Bucket'], Key=create_multipart_upload_response['Key'],
                                              UploadId=create_multipart_upload_response['UploadId'], PartNumber=part_number, Body=part_body)
        parts.append({'PartNumber': part_number, 'ETag': upload_part_response['ETag']})
        part_number += 1
        uploaded += len(part_body)
complete_multipart_upload_response = s3.complete_multipart_upload(
    Bucket=create_multipart_upload_response['Bucket'], Key=create_multipart_upload_response['Key'], UploadId=create_multipart_upload_response['UploadId'], MultipartUpload={'Parts': parts})
print('ETag:', complete_multipart_upload_response['ETag'])

UploadObject

Create main.py

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
s3.upload_file("<path/to/upload>", "<Bucket>", "<Key>")
print('Done')

GetObject

Client-side get object

Create main.py

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
presigned_url = s3.generate_presigned_url(ClientMethod='get_object', Params={'Bucket': '<Bucket>', 'Key': '<Key>'})
print(presigned_url)

This code will generate a pre-signed client-side download URL, valid for 1 hour, which the client can use to send an GET request and download the file within the expiration time. The following is an example of downloading a file using curl:

curl -o "<path/to/download>" "<presigned url>"

You can also specify the expiration time for the download credentials, for example:

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
presigned_url = s3.generate_presigned_url(ClientMethod='get_object', Params={'Bucket': '<Bucket>', 'Key': '<Key>'}, ExpiresIn=86400)
print(presigned_url)

Server-side get object

Create main.py

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
s3.download_file('<Bucket>', '<Key>', '<path/to/download>')
print('Done')

ObjectOperations

HeadObject

Create main.py

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
head_object_response = s3.head_object(Bucket='<Bucket>', Key='<Key>')
print('ETag:', head_object_response['ETag'])

ChangeStorageClass

Create main.py

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
s3.copy_object(Bucket='<Bucket>', Key='<Key>', CopySource='/<Bucket>/<Key>', MetadataDirective='REPLACE', StorageClass='GLACIER')
print('Done')

CopyObject

Create main.py

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
s3.copy_object(Bucket='<ToBucket>', Key='<ToKey>', CopySource='/<FromBucket>/<FromKey>', MetadataDirective='COPY')
print('Done')

CopyObject(> 5GB)

Create main.py

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
head_object_response = s3.head_object(
    Bucket='<FromBucket>', Key='<FromKey>')
create_multipart_upload_response = s3.create_multipart_upload(
    Bucket='<ToBucket>', Key='<ToKey>')
uploaded = 0
PART_SIZE = 5*1024*1024  # part size is 5 MB
part_number = 1
parts = []
copied = 0
# The example given here is a serial multipart copy. You can modify it to perform a parallel multipart copy to further improve the copy speed.
while copied < head_object_response['ContentLength']:
    part_size = min(head_object_response['ContentLength'] - copied, PART_SIZE)
    upload_part_copy_response = s3.upload_part_copy(Bucket=create_multipart_upload_response['Bucket'], Key=create_multipart_upload_response['Key'],
                                                    UploadId=create_multipart_upload_response['UploadId'], PartNumber=part_number, CopySource='/<FromBucket>/<FromKey>', CopySourceRange=f'bytes={copied}-{copied+part_size}')
    parts.append({'PartNumber': part_number,
                 'ETag': upload_part_copy_response['CopyPartResult']['ETag']})
    part_number += 1
    copied += part_size
complete_multipart_upload_response = s3.complete_multipart_upload(
    Bucket=create_multipart_upload_response['Bucket'], Key=create_multipart_upload_response['Key'], UploadId=create_multipart_upload_response['UploadId'], MultipartUpload={'Parts': parts})
print('ETag:', complete_multipart_upload_response['ETag'])

DeleteObject

Create main.py

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
s3.delete_object(Bucket='<Bucket>', Key='<Key>')
print('Done')

ListObjects

Create main.py

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
response = s3.list_objects_v2(Bucket='<Bucket>', Prefix='<KeyPrefix>')
for object in response['Contents']:
    print('Key:', object['Key'])
    print('ETag:', object['ETag'])

DeleteObjects

Create main.py

import boto3

s3 = boto3.client('s3',
                  region_name='ap-southeast-2", # Asia Pacific (Hanoi) RegionID
                  endpoint_url='https://mos.ap-southeast-2.sufybkt.com", # Asia Pacific (Hanoi) Endpoint
                  aws_access_key_id='<AccessKey>',
                  aws_secret_access_key='<SecretKey>',
                  config=boto3.session.Config(signature_version="s3v4"))
s3.delete_objects(Bucket='<Bucket>',
                  Delete={'Objects': [{'Key': '<Key1>'}, {'Key': '<Key2>'}, {'Key': '<Key3>'}]})
print('Done')