public class S3Link
extends java.lang.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
AmazonS3Client
and TransferManager
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 |
---|---|
(package private) static class |
S3Link.ID
JSON wrapper of an
S3Link identifier,
which consists of the S3 region id, bucket name and key. |
private static class |
S3Link.S3
Internal class for JSON serialization purposes.
|
Modifier and Type | Field and Description |
---|---|
private S3Link.ID |
id |
private S3ClientCache |
s3cc |
Modifier | Constructor and Description |
---|---|
(package private) |
S3Link(S3ClientCache s3cc,
Region region,
java.lang.String bucketName,
java.lang.String key) |
private |
S3Link(S3ClientCache s3cc,
S3Link.ID id) |
(package private) |
S3Link(S3ClientCache s3cc,
java.lang.String bucketName,
java.lang.String key) |
Modifier and Type | Method and Description |
---|---|
ObjectMetadata |
downloadTo(java.io.File destination)
Convenient method to synchronously download to the specified file from
the S3 object represented by this S3Link.
|
ObjectMetadata |
downloadTo(java.io.File destination,
RequestMetricCollector requestMetricCollector)
Same as
downloadTo(File) but allows specifying a
request metric collector. |
ObjectMetadata |
downloadTo(java.io.OutputStream output)
Downloads the data from the object represented by this S3Link to the
specified output stream.
|
ObjectMetadata |
downloadTo(java.io.OutputStream output,
RequestMetricCollector requestMetricCollector)
Same as
downloadTo(OutputStream) but allows specifying a
request metric collector. |
private ObjectMetadata |
downloadTo0(java.io.File destination,
RequestMetricCollector requestMetricCollector) |
private ObjectMetadata |
downloadTo0(java.io.OutputStream output,
RequestMetricCollector requestMetricCollector) |
static S3Link |
fromJson(S3ClientCache s3cc,
java.lang.String json)
Deserializes from a JSON string.
|
AmazonS3 |
getAmazonS3Client() |
java.lang.String |
getBucketName() |
java.lang.String |
getKey() |
Region |
getS3Region() |
TransferManager |
getTransferManager() |
java.net.URL |
getUrl()
Returns a URL for the location of the object represented by this S3Link.
|
void |
setAcl(AccessControlList acl)
Sets the access control list for the object represented by this S3Link.
|
void |
setAcl(AccessControlList acl,
RequestMetricCollector requestMetricCollector)
Same as
setAcl(AccessControlList) but allows specifying a
request metric collector. |
void |
setAcl(CannedAccessControlList acl)
Sets the access control list for the object represented by this S3Link.
|
void |
setAcl(CannedAccessControlList acl,
RequestMetricCollector col) |
private void |
setAcl0(AccessControlList acl,
RequestMetricCollector requestMetricCollector) |
private void |
setAcl0(CannedAccessControlList acl,
RequestMetricCollector col) |
java.lang.String |
toJson()
Serializes into a JSON string.
|
PutObjectResult |
uploadFrom(byte[] buffer)
Convenience method to synchronously upload from the given buffer to the
Amazon S3 object represented by this S3Link.
|
PutObjectResult |
uploadFrom(byte[] buffer,
RequestMetricCollector requestMetricCollector)
Same as
uploadFrom(byte[]) but allows specifying a
request metric collector. |
PutObjectResult |
uploadFrom(java.io.File source)
Convenience method to synchronously upload from the given file to the
Amazon S3 object represented by this S3Link.
|
PutObjectResult |
uploadFrom(java.io.File source,
RequestMetricCollector requestMetricCollector)
Same as
uploadFrom(File) but allows specifying a
request metric collector. |
private PutObjectResult |
uploadFrom0(byte[] buffer,
RequestMetricCollector requestMetricCollector) |
private PutObjectResult |
uploadFrom0(java.io.File source,
RequestMetricCollector requestMetricCollector) |
private final S3ClientCache s3cc
private final S3Link.ID id
S3Link(S3ClientCache s3cc, java.lang.String bucketName, java.lang.String key)
S3Link(S3ClientCache s3cc, Region region, java.lang.String bucketName, java.lang.String key)
private S3Link(S3ClientCache s3cc, S3Link.ID id)
public java.lang.String getKey()
public java.lang.String getBucketName()
public Region getS3Region()
public java.lang.String toJson()
public static S3Link fromJson(S3ClientCache s3cc, java.lang.String json)
public AmazonS3 getAmazonS3Client()
public TransferManager getTransferManager()
public PutObjectResult uploadFrom(java.io.File source)
source
- source file to upload fromPutObjectResult
object containing the information
returned by Amazon S3 for the newly created object.public PutObjectResult uploadFrom(java.io.File source, RequestMetricCollector requestMetricCollector)
uploadFrom(File)
but allows specifying a
request metric collector.private PutObjectResult uploadFrom0(java.io.File source, RequestMetricCollector requestMetricCollector)
public PutObjectResult uploadFrom(byte[] buffer)
buffer
- The buffer containing the data to upload.PutObjectResult
object containing the information
returned by Amazon S3 for the newly created object.public PutObjectResult uploadFrom(byte[] buffer, RequestMetricCollector requestMetricCollector)
uploadFrom(byte[])
but allows specifying a
request metric collector.private PutObjectResult uploadFrom0(byte[] buffer, RequestMetricCollector requestMetricCollector)
public void setAcl(CannedAccessControlList acl)
acl
- The access control list describing the new permissions for the
object represented by this S3Link.public void setAcl(CannedAccessControlList acl, RequestMetricCollector col)
private void setAcl0(CannedAccessControlList acl, RequestMetricCollector col)
public void setAcl(AccessControlList acl)
acl
- The access control list describing the new permissions for the
object represented by this S3Link.public void setAcl(AccessControlList acl, RequestMetricCollector requestMetricCollector)
setAcl(AccessControlList)
but allows specifying a
request metric collector.private void setAcl0(AccessControlList acl, RequestMetricCollector requestMetricCollector)
public java.net.URL getUrl()
If the object represented by this S3Link has public read permissions (ex:
CannedAccessControlList.PublicRead
), then this URL can be
directly accessed to retrieve the object data.
public ObjectMetadata downloadTo(java.io.File destination)
destination
- destination file to download topublic ObjectMetadata downloadTo(java.io.File destination, RequestMetricCollector requestMetricCollector)
downloadTo(File)
but allows specifying a
request metric collector.private ObjectMetadata downloadTo0(java.io.File destination, RequestMetricCollector requestMetricCollector)
public ObjectMetadata downloadTo(java.io.OutputStream output)
output
- The output stream to write the object's data to.public ObjectMetadata downloadTo(java.io.OutputStream output, RequestMetricCollector requestMetricCollector)
downloadTo(OutputStream)
but allows specifying a
request metric collector.private ObjectMetadata downloadTo0(java.io.OutputStream output, RequestMetricCollector requestMetricCollector)