How to write a file or data to an S3 object using boto3

后端 未结 7 830
一生所求
一生所求 2020-11-28 21:21

In boto 2, you can write to an S3 object using these methods:

  • Key.set_contents_from_string()
  • Key.set_contents_from_file()
  • Key.set_contents_fr
7条回答
  •  一整个雨季
    2020-11-28 22:01

    A cleaner and concise version which I use to upload files on the fly to a given S3 bucket and sub-folder-

    import boto3
    
    BUCKET_NAME = 'sample_bucket_name'
    PREFIX = 'sub-folder/'
    
    s3 = boto3.resource('s3')
    
    # Creating an empty file called "_DONE" and putting it in the S3 bucket
    s3.Object(BUCKET_NAME, PREFIX + '_DONE').put(Body="")
    

    Note: You should ALWAYS put your AWS credentials (aws_access_key_id and aws_secret_access_key) in a separate file, for example- ~/.aws/credentials

提交回复
热议问题