Skip to content

Instantly share code, notes, and snippets.

@ahumeau
Last active February 27, 2024 16:03
Show Gist options
  • Save ahumeau/5b58e3ee0f77f52572ae825e85c2cbb9 to your computer and use it in GitHub Desktop.
Save ahumeau/5b58e3ee0f77f52572ae825e85c2cbb9 to your computer and use it in GitHub Desktop.
import io
import os
import boto3
from botocore.config import Config
from storages.utils import ReadBytesWrapper
S3_BUCKET_NAME = os.environ["S3_BUCKET_NAME"]
FILE_KEY = "test"
FILE_CONTENT = "é"
def main():
# boto3 will look for your credentials in the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN
# env variables.
session = boto3.Session()
connection = session.resource(
"s3",
config=Config(
retries={"max_attempts": 1, "mode": "standard"},
),
)
bucket = connection.Bucket(S3_BUCKET_NAME)
obj = bucket.Object(FILE_KEY)
content = io.StringIO(FILE_CONTENT)
content = ReadBytesWrapper(content)
# Defining `content` with the following line works.
# content = io.BytesIO(FILE_CONTENT.encode("utf-8"))
obj.upload_fileobj(content)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment