Home > Software engineering >  uploading a file to S3, using boto3. i don't know what to replace the <FMI> with
uploading a file to S3, using boto3. i don't know what to replace the <FMI> with

Time:09-30

import boto3
S3API = boto3.client("s3", region_name="us-east-1")
S3API.**<FMI>**(Bucket="tiwaladebucket")

import boto3
S3API = boto3.client("s3", region_name="us-east-1") 
bucket_name = "tiwaladebucketcatfound"

filename = "../cat.jpg"
S3API.**<FMI>**(filename, tiwaladebucketcatfound, "cat.jpg", ExtraArgs={'**<FMI>**': "image/jpg", "CacheControl": "max-age=0"})

filename = "../index.html"
S3API.**<FMI>**(filename, tiwaladebucketcatfound, "**<FMI>**", ExtraArgs={'ContentType': "text/html", "CacheControl": "max-age=0"})

CodePudding user response:

You are s3 client and need to use put_object API to upload files to S3

S3API.put_object(Body=binary_data, Bucket='my_bucket_name', Key='s3folder/path/filename.txt')

or 

S3API.put_object(Body=file_object, Bucket='my_bucket_name', Key='s3folder/path/filename.txt')

Reference: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.put_object

Alternatively, you can use S3 resource instead of S3 Client and perform the same

s3_resource = boto3.resource('s3')    
s3_resource.Bucket('bucketname').upload_file('/local/file/a.txt','folder/path/to/s3key')

CodePudding user response:

There are multiple FMI and as I can see, they are not to be replaced by the same command. More information about what the code is trying would be nice, but my guess is that you want to:

  • create the bucket "iwaladebucket"
  • upload two files to said bucket "../index.html" and "../cat.jpg"

Using your code from above, the following would achieve this:

import boto3
s3client = boto3.client("s3", region_name="us-east-1")
s3client.create_bucket(Bucket="tiwaladebucket")

bucket_name = "tiwaladebucket"

filename = "../cat.jpg"
s3client.put_object(filename, bucket_name, "cat.jpg", ExtraArgs={'ContentType': "image/jpg", "CacheControl": "max-age=0"})

filename = "../index.html"
s3client.put_object(filename, bucket_name, "index.html", ExtraArgs={'ContentType': "text/html", "CacheControl": "max-age=0"})
  • Related