Skip to content

Python Script COS Bucket Upload and Download Example

Amitabha Biswas edited this page Jun 17, 2020 · 4 revisions
import ibm_boto3
from ibm_botocore.client import Config
from ibm_s3transfer.aspera.manager import AsperaTransferManager
from ibm_s3transfer.aspera.manager import AsperaConfig

COS_ENDPOINT = "https://s3.us-south.cloud-object-storage.appdomain.cloud" # Current list avaiable at https://control.cloud-object-storage.cloud.ibm.com/v2/endpoints
COS_API_KEY_ID = "0Kxxxxxxxxxxx"
COS_AUTH_ENDPOINT = "https://iam.cloud.ibm.com/identity/token"
COS_RESOURCE_CRN = "xxx-xx-xx-xx-xxx"
COS_BUCKET_LOCATION = "vnfimages"

# Create resource
client = ibm_boto3.client("s3",
    ibm_api_key_id=COS_API_KEY_ID,
    ibm_service_instance_id=COS_RESOURCE_CRN,
    ibm_auth_endpoint=COS_AUTH_ENDPOINT,
    config=Config(signature_version="oauth"),
    endpoint_url=COS_ENDPOINT
)

# Create the Aspera Transfer Manager
ms_transfer_config = AsperaConfig(multi_session=2,
                                  multi_session_threshold_mb=100)
# Create the Aspera Transfer Manager
transfer_manager = AsperaTransferManager(client=client,
                                         transfer_config=ms_transfer_config)
                                         
def download_file():
    bucket_name = "vnfimages"
    download_filename = "./image.qcow2"
    object_name = "image.qcow2"
    # perform download
    future = transfer_manager.download(bucket_name, object_name, download_filename)
    # Wait for download to complete
    future.result()                                         

def upload_file():    
    bucket_name = "nfvconfigvault"
    upload_filename = "./image.qcow2"
    object_name = "image.qcow2"
    # Perform upload
    future = transfer_manager.upload(upload_filename, bucket_name, object_name)
    # Wait for upload to complete
    future.result()

def main(**kwargs):
    """
    The method that is invoked, when run with CLI arguments

    :param kwargs:
    :return: void
    """
    try:
        print('Downloading file from COS bucket') 
        download_file()           
        print('File Downloaded')   
        print('Uploading file to COS bucket')    
        upload_file()
    except Exception as e:
        print(Exception, e)
    else:
        print('File Uploaded')
        
        
if __name__ == '__main__':
    main()
Clone this wiki locally