attempting to upload file with boto3 from same script it was created in but getting error: No such file or directory

Viewed 733

here is the code

with open('browse_venues_current_content.json', 'w') as my_file:
  json.dump(endproduct, my_file)

 import boto3
 s3 = boto3.resource('s3')
 result = s3.meta.client.upload_file('my_file', 'shofi-stuff', 'browse_venues_current_content.json')
 print(result)

here is the error:

 line 50, in getsize
    return os.stat(filename).st_size
FileNotFoundError: [Errno 2] No such file or directory: 'my_file'

what should I be doing?

2 Answers

looks like this is my winner

The upload_fileobj method accepts a readable file-like object. The file object must be opened in binary mode, not text mode.

s3 = boto3.client('s3')
with open("FILE_NAME", "rb") as f:
    s3.upload_fileobj(f, "BUCKET_NAME", "OBJECT_NAME")

The upload_file and upload_fileobj methods are provided by the S3 Client, Bucket, and Object classes. The method functionality provided by each class is identical. No benefits are gained by calling one class's method over another's. Use whichever class is most convenient.

You should use browse_venues_current_content.json not "my_file":

endproduct = [1,2,3]

with open('browse_venues_current_content.json', 'w') as my_file:
    json.dump(endproduct, my_file)


s3 = boto3.resource('s3')

result = s3.meta.client.upload_file(
        'browse_venues_current_content.json', 
        'shofi-stuff', 
        'browse_venues_current_content.json')

print(result)
Related