-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3.py
More file actions
36 lines (26 loc) · 950 Bytes
/
s3.py
File metadata and controls
36 lines (26 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import boto3
import os
from dotenv import load_dotenv
load_dotenv()
# Initialize the S3 client
s3 = boto3.client('s3',
aws_access_key_id=os.getenv('aws_access_key_id'),
aws_secret_access_key=os.getenv('aws_secret_access_key')
)
# Your bucket and access point name
BUCKET_NAME = os.getenv('BUCKET_NAME')
# Upload a file
file_name = 'test.txt'
with open(file_name, 'wb') as f:
f.write(b'This is a test file')
s3.upload_file(file_name, BUCKET_NAME, file_name, ExtraArgs={"ACL": "bucket-owner-full-control"})
print(f'Uploaded {file_name} to {BUCKET_NAME}')
# Download the file
download_path = 'downloaded_test.txt'
s3.download_file(BUCKET_NAME, file_name, download_path)
print(f'Downloaded {file_name} to {download_path}')
# Ensure the content matches
with open(download_path, 'rb') as f:
content = f.read()
assert content == b'This is a test file'
print('File content matches!')