Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 545
nos Avatar asked Oct 20 '25 05:10

nos


1 Answers

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'
  }
)
like image 183
John Rotenstein Avatar answered Oct 21 '25 20:10

John Rotenstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!