简体   繁体   中英

How to use boto3 to write to S3 standard infrequent access?

I searched in the boto3 doc but didn't find relevant information there. In this link , it is mentioned that it can be done using

k.storage_class='STANDARD_IA'

Can someone share a full code snippet here? Many thanks.

New file

import boto3

client = boto3.client('s3')

client.upload_file(
    Filename = '/tmp/foo.txt', 
    Bucket = 'my-bucket', 
    Key = 'foo.txt',
    ExtraArgs = {
      'StorageClass': 'STANDARD_IA'
    }
)

Existing file

From How to change storage class of existing key via boto3 :

import boto3

s3 = boto3.client('s3')

copy_source = {
    'Bucket': 'mybucket',
    'Key': 'mykey'
}

s3.copy(
  CopySource = copy_source,
  Bucket = 'target-bucket', 
  Key = 'target-key',
  ExtraArgs = {
    'StorageClass': 'STANDARD_IA',
    'MetadataDirective': 'COPY'
  }
)

From the boto3 Storing Data example, it looks like the standard way to put objects in boto3 is

s3.Object('mybucket', 'hello.txt').put(Body=open('/tmp/hello.txt', 'rb'))

But to set the storage class, S3.Object.Put suggests we'd want to use parameter:

StorageClass='STANDARD_IA'

So combining the two, we have:

import boto3
s3 = boto3.resource('s3')
s3.Object('mybucket', 'hello.txt').put(Body=open('/tmp/hello.txt', 'rb'), StorageClass='STANDARD_IA')

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM