0

I know how to upload a file to s3 buckets in Python. I am looking for a way to upload data to a file in s3 bucket directly. In this way, I do not need to save my data to a local file, and then upload the file. Any suggestions? Thanks!

5
  • FYI, most issues I see on StackOverflow is when people try to do it this way. It is normally much safer to upload_file() rather than send data to a file. Commented Dec 17, 2018 at 20:25
  • @snakecharmerb I do not find any way to to it yet Commented Dec 17, 2018 at 20:54
  • @John Rotenstein. Do you know why uploading a file is safer? Commented Dec 17, 2018 at 20:55
  • 1
    I think people make more mistakes where code is more complex. It is simpler to write code that creates a file, then uploads a file. When people try to do both in the same step, they run into more problems. It is also easier to test code that does it in separate steps. However, @CharlesLandau's method will probably work (note that it uses the Resource method instead of the Client method). Commented Dec 17, 2018 at 21:05
  • @JohnRotenstein I am editing to include client method, I thought it would be fine to just point out one Commented Dec 17, 2018 at 22:21

1 Answer 1

1

AFAIK standard Object.put() supports this.

resp = s3.Object('bucket_name', 'key/key.txt').put(Body=b'data')

Edit: it was pointed out that you might want the client method, which is just put_object with the kwargs differently organized

client.put_object(Body=b'data', Bucket='bucket_name', Key='key/key.txt')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.