public class S3Link extends Object
DynamoDbMapper
.
An S3 link is persisted as a JSON string in DynamoDB.
This link object can be used directly to upload/download files to S3.
Alternatively, the underlying
S3Client
can be retrieved to
provide full access API to S3.
For example:
AWSCredentialsProvider s3CredentialProvider = ...; DynamoDBMapper mapper = new DynamoDBMapper(..., s3CredentialProvider); String username = "jamestkirk"; User user = new User(); user.setUsername(username); // S3 region can be specified, but is optional S3Link s3link = mapper.createS3Link("my-company-user-avatars", username + ".jpg"); user.setAvatar(s3link); // All meta information of the S3 resource is persisted in DynamoDB, including // region, bucket, and key mapper.save(user); // Upload file to S3 with the link saved in DynamoDB s3link.uploadFrom(new File("/path/to/all/those/user/avatars/" + username + ".jpg")); // Download file from S3 via an S3Link s3link.downloadTo(new File("/path/to/downloads/" + username + ".jpg")); // Full S3 API is available via the canonical AmazonS3Client and TransferManager API. // For example: AmazonS3Client s3 = s3link.getAmazonS3Client(); TransferManager s3m = s3link.getTransferManager(); // etc.The User pojo class used above:
@DynamoDBTable(tableName = "user-table") public class User { private String username; private S3Link avatar; @DynamoDBHashKey public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public S3Link getAvatar() { return avatar; } public void setAvatar(S3Link avatar) { this.avatar = avatar; } }
Modifier and Type | Class and Description |
---|---|
static class |
S3Link.Factory
S3Link factory. |
Modifier and Type | Method and Description |
---|---|
String |
bucketName() |
GetObjectResponse |
downloadTo(File destination)
Convenient method to synchronously download to the specified file from
the S3 object represented by this S3Link.
|
GetObjectResponse |
downloadTo(File destination,
RequestMetricCollector requestMetricCollector)
Same as
downloadTo(File) but allows specifying a
request metric collector. |
GetObjectResponse |
downloadTo(OutputStream output)
Downloads the data from the object represented by this S3Link to the
specified output stream.
|
GetObjectResponse |
downloadTo(OutputStream output,
RequestMetricCollector requestMetricCollector)
Same as
downloadTo(OutputStream) but allows specifying a
request metric collector. |
static S3Link |
fromJson(S3ClientCache s3cc,
String json)
Deserializes from a JSON string.
|
S3Client |
getAmazonS3Client() |
String |
getKey() |
String |
getRegion()
Returns the S3 region as string.
|
Region |
s3Region()
Returns the S3 region in
Region format. |
void |
setAcl(AccessControlPolicy acl,
RequestMetricCollector requestMetricCollector) |
void |
setAcl(ObjectCannedACL acl)
Sets the access control list for the object represented by this S3Link.
|
void |
setAcl(ObjectCannedACL acl,
RequestMetricCollector col) |
String |
toJson()
Serializes into a JSON string.
|
PutObjectResponse |
uploadFrom(byte[] buffer)
Convenience method to synchronously upload from the given buffer to the
Amazon S3 object represented by this S3Link.
|
PutObjectResponse |
uploadFrom(byte[] buffer,
RequestMetricCollector requestMetricCollector)
Same as
uploadFrom(byte[]) but allows specifying a
request metric collector. |
PutObjectResponse |
uploadFrom(File source)
Convenience method to synchronously upload from the given file to the
Amazon S3 object represented by this S3Link.
|
PutObjectResponse |
uploadFrom(File source,
RequestMetricCollector requestMetricCollector)
Same as
uploadFrom(File) but allows specifying a
request metric collector. |
public static S3Link fromJson(S3ClientCache s3cc, String json)
public String getKey()
public String bucketName()
public Region s3Region()
Region
format.
Do not use this method if S3Link
is created with a region not in Region
enum.
Use getRegion()
instead.
public String getRegion()
public String toJson()
public S3Client getAmazonS3Client()
public PutObjectResponse uploadFrom(File source)
source
- source file to upload fromPutObjectResponse
object containing the information
returned by Amazon S3 for the newly created object.public PutObjectResponse uploadFrom(File source, RequestMetricCollector requestMetricCollector)
uploadFrom(File)
but allows specifying a
request metric collector.public PutObjectResponse uploadFrom(byte[] buffer)
buffer
- The buffer containing the data to upload.PutObjectResponse
object containing the information
returned by Amazon S3 for the newly created object.public PutObjectResponse uploadFrom(byte[] buffer, RequestMetricCollector requestMetricCollector)
uploadFrom(byte[])
but allows specifying a
request metric collector.public void setAcl(ObjectCannedACL acl)
acl
- The access control list describing the new permissions for the
object represented by this S3Link.public void setAcl(ObjectCannedACL acl, RequestMetricCollector col)
public void setAcl(AccessControlPolicy acl, RequestMetricCollector requestMetricCollector)
public GetObjectResponse downloadTo(File destination)
destination
- destination file to download topublic GetObjectResponse downloadTo(File destination, RequestMetricCollector requestMetricCollector)
downloadTo(File)
but allows specifying a
request metric collector.public GetObjectResponse downloadTo(OutputStream output)
output
- The output stream to write the object's data to.public GetObjectResponse downloadTo(OutputStream output, RequestMetricCollector requestMetricCollector)
downloadTo(OutputStream)
but allows specifying a
request metric collector.Copyright © 2017. All rights reserved.