dark horse
dark horse

Reputation: 3709

Python - Unable to upload file to defined folder in AWS S3 bucket

I am trying to upload a csv file from my local to aws s3 bucket. Given below is the code I am using but it doesn't seen to upload the file to the s3 folder defined. Could anyone assist.

import boto3
from botocore.client import Config

ACCESS_KEY_ID = 'accesskeyid'
ACCESS_SECRET_KEY = 'secretkeyid'
BUCKET_NAME = 'bucketname'

data = open('/desktop/file.csv', 'rb')

s3 = boto3.resource(
's3',
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=ACCESS_SECRET_KEY,
config=Config(signature_version='s3v4')
)
s3.Bucket(BUCKET_NAME).put_object(Key='/sub-folder/sub-folder2/file.csv', Body=data)

print ("Uploaded successfully")

Could anyone help me in finding where I am going wrong. Thanks

Upvotes: 1

Views: 571

Answers (1)

ame
ame

Reputation: 456

You need to remove the / from the beginning of the Key argument.

Using your existing code, the file path will be: BUCKET_NAME//sub-folder/sub-folder2/file.csv.

Upvotes: 1

Related Questions