s3example.java
· 2.0 KiB · Java
Raw
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import java.io.File;
public class S3Examples {
private final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
// Create an S3 bucket
public void createBucket(String bucketName) {
s3.createBucket(bucketName);
}
// Upload an object to S3
public void uploadObject(String bucketName, String key, File file) {
s3.putObject(bucketName, key, file);
}
// Download an object from S3
public void downloadObject(String bucketName, String key, File file) {
s3.getObject(new GetObjectRequest(bucketName, key), file);
}
// List objects in a bucket
public ObjectListing listObjects(String bucketName) {
return s3.listObjects(bucketName);
}
// Generate pre-signed URL to share an S3 object
public URL generatePresignedUrl(String bucketName, String key) {
Date expiration = new Date();
long expTimeMillis = expiration.getTime();
expTimeMillis += 1000 * 60 * 60; // Add 1 hour.
expiration.setTime(expTimeMillis);
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(bucketName, key)
.withMethod(HttpMethod.GET)
.withExpiration(expiration);
return s3.generatePresignedUrl(generatePresignedUrlRequest);
}
}
// The key steps are:
// 1. Create an S3Client object to interact with S3
// 2. Call createBucket() to create a new S3 bucket
// 3. Upload objects using putObject(), specifying bucket name, key, and file
// 4. Download objects using getObject(), specifying bucket name, key, and target file
// 5. List objects in a bucket using listObjects()
// 6. Generate a pre-signed URL using generatePresignedUrl() by specifying bucket,
// key, expiration date, and HTTP method.
// The pre-signed URL allows temporary access to private S3 objects without requiring AWS credentials.
// The URL is only valid until the specified expiration date/time.
1 | import com.amazonaws.services.s3.AmazonS3; |
2 | import com.amazonaws.services.s3.AmazonS3ClientBuilder; |
3 | import com.amazonaws.services.s3.model.*; |
4 | import java.io.File; |
5 | |
6 | public class S3Examples { |
7 | |
8 | private final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); |
9 | |
10 | // Create an S3 bucket |
11 | public void createBucket(String bucketName) { |
12 | s3.createBucket(bucketName); |
13 | } |
14 | |
15 | // Upload an object to S3 |
16 | public void uploadObject(String bucketName, String key, File file) { |
17 | s3.putObject(bucketName, key, file); |
18 | } |
19 | |
20 | // Download an object from S3 |
21 | public void downloadObject(String bucketName, String key, File file) { |
22 | s3.getObject(new GetObjectRequest(bucketName, key), file); |
23 | } |
24 | |
25 | // List objects in a bucket |
26 | public ObjectListing listObjects(String bucketName) { |
27 | return s3.listObjects(bucketName); |
28 | } |
29 | |
30 | // Generate pre-signed URL to share an S3 object |
31 | public URL generatePresignedUrl(String bucketName, String key) { |
32 | Date expiration = new Date(); |
33 | long expTimeMillis = expiration.getTime(); |
34 | expTimeMillis += 1000 * 60 * 60; // Add 1 hour. |
35 | expiration.setTime(expTimeMillis); |
36 | |
37 | GeneratePresignedUrlRequest generatePresignedUrlRequest = |
38 | new GeneratePresignedUrlRequest(bucketName, key) |
39 | .withMethod(HttpMethod.GET) |
40 | .withExpiration(expiration); |
41 | |
42 | return s3.generatePresignedUrl(generatePresignedUrlRequest); |
43 | } |
44 | } |
45 | // The key steps are: |
46 | |
47 | // 1. Create an S3Client object to interact with S3 |
48 | // 2. Call createBucket() to create a new S3 bucket |
49 | // 3. Upload objects using putObject(), specifying bucket name, key, and file |
50 | // 4. Download objects using getObject(), specifying bucket name, key, and target file |
51 | // 5. List objects in a bucket using listObjects() |
52 | // 6. Generate a pre-signed URL using generatePresignedUrl() by specifying bucket, |
53 | // key, expiration date, and HTTP method. |
54 | |
55 | // The pre-signed URL allows temporary access to private S3 objects without requiring AWS credentials. |
56 | // The URL is only valid until the specified expiration date/time. |
s3example.py
· 1.1 KiB · Python
Raw
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).
1 | import boto3 |
2 | from botocore.exceptions import ClientError |
3 | |
4 | s3 = boto3.client('s3') |
5 | |
6 | # Create a bucket |
7 | bucket_name = 'my-bucket' |
8 | location = {'LocationConstraint':'us-east-1'} |
9 | s3.create_bucket(Bucket=bucket_name, CreateBucketConfiguration=location) |
10 | |
11 | # Upload an object to bucket |
12 | filename = 'data.csv' |
13 | object_name = 'data/data.csv' |
14 | s3.upload_file(filename, bucket_name, object_name) |
15 | |
16 | # List objects in bucket |
17 | response = s3.list_objects(Bucket=bucket_name) |
18 | for object in response['Contents']: |
19 | print(object['Key']) |
20 | |
21 | # Download an object |
22 | s3.download_file(bucket_name, object_name, 'data_download.csv') |
23 | |
24 | # Generate pre-signed URL to share an object |
25 | url = s3.generate_presigned_url( |
26 | ClientMethod='get_object', |
27 | Params={'Bucket': bucket_name, 'Key': object_name}, |
28 | ExpiresIn=3600) |
29 | |
30 | print(url) |
31 | |
32 | # This creates an S3 bucket, uploads a local CSV file to the bucket under object name 'data/data.csv', |
33 | # lists all objects in the bucket, downloads the object to a local file, and generates a pre-signed URL |
34 | # that allows temporary access to download the object for anyone with the URL. |
35 | # The pre-signed URL expires after 3600 seconds (1 hour). |
36 |