-
Notifications
You must be signed in to change notification settings - Fork 1
/
S3Examples.py
35 lines (27 loc) · 1.12 KB
/
S3Examples.py
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
from botocore.exceptions import ClientError
s3 = boto3.client('s3')
# Create a bucket
bucket_name = 'my-bucket'
location = {'LocationConstraint':'us-east-1'}
s3.create_bucket(Bucket=bucket_name, CreateBucketConfiguration=location)
# Upload an object to bucket
filename = 'data.csv'
object_name = 'data/data.csv'
s3.upload_file(filename, bucket_name, object_name)
# List objects in bucket
response = s3.list_objects(Bucket=bucket_name)
for object in response['Contents']:
print(object['Key'])
# Download an object
s3.download_file(bucket_name, object_name, 'data_download.csv')
# Generate pre-signed URL to share an object
url = s3.generate_presigned_url(
ClientMethod='get_object',
Params={'Bucket': bucket_name, 'Key': object_name},
ExpiresIn=3600)
print(url)
# This creates an S3 bucket, uploads a local CSV file to the bucket under object name 'data/data.csv',
# lists all objects in the bucket, downloads the object to a local file, and generates a pre-signed URL
# that allows temporary access to download the object for anyone with the URL.
# The pre-signed URL expires after 3600 seconds (1 hour).