Example usage for com.amazonaws.services.s3.model ObjectMetadata getUserMetadata

List of usage examples for com.amazonaws.services.s3.model ObjectMetadata getUserMetadata

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model ObjectMetadata getUserMetadata.

Prototype

public Map<String, String> getUserMetadata() 

Source Link

Document

Gets the custom user-metadata for the associated object.

Usage

From source file:com.altoukhov.svsync.fileviews.S3FileSpace.java

License:Apache License

@Override
public boolean writeFile(InputStream fileStream, FileSnapshot file) {
    if (fileStream == null)
        return false;

    if (file.isLargeFile()) {
        return writeLargeFile(fileStream, file);
    }//from  w  w w . ja  v  a  2  s.co  m

    try {
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(file.getFileSize());
        meta.getUserMetadata().put("lmd", file.getModifiedTimestamp().toDate().getTime() + "");
        meta.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
        s3.putObject(bucketName, toAbsoluteFilePath(file.getRelativePath()), fileStream, meta);
    } catch (AmazonClientException ex) {
        return false;
    }
    return true;
}

From source file:com.altoukhov.svsync.fileviews.S3FileSpace.java

License:Apache License

public boolean writeLargeFile(InputStream fileStream, FileSnapshot file) {
    if (fileStream == null)
        return false;

    try {/*from   w ww .  ja v  a2 s.c om*/
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(file.getFileSize());
        meta.getUserMetadata().put("lmd", file.getModifiedTimestamp().toDate().getTime() + "");
        meta.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);

        List<PartETag> partTags = new ArrayList<>();
        String fileKey = toAbsoluteFilePath(file.getRelativePath());

        InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, fileKey, meta);
        InitiateMultipartUploadResult result = s3.initiateMultipartUpload(request);

        long contentLength = file.getFileSize();
        long partSize = 256 * 1024 * 1024;

        try {
            // Uploading the file, part by part.
            long filePosition = 0;

            for (int i = 1; filePosition < contentLength; i++) {

                partSize = Math.min(partSize, (contentLength - filePosition));

                // Creating the request for a part upload
                UploadPartRequest uploadRequest = new UploadPartRequest().withBucketName(bucketName)
                        .withKey(fileKey).withUploadId(result.getUploadId()).withPartNumber(i)
                        .withInputStream(fileStream).withPartSize(partSize);

                // Upload part and add response to the result list.
                partTags.add(s3.uploadPart(uploadRequest).getPartETag());
                filePosition += partSize;

                System.out.println("Uploaded " + Utils.readableFileSize(filePosition) + " out of "
                        + Utils.readableFileSize(contentLength));
            }
        } catch (Exception e) {
            System.out.println("UploadPartRequest failed: " + e.getMessage());
            s3.abortMultipartUpload(new AbortMultipartUploadRequest(bucketName, fileKey, result.getUploadId()));
            return false;
        }

        s3.completeMultipartUpload(
                new CompleteMultipartUploadRequest(bucketName, fileKey, result.getUploadId(), partTags));
    } catch (AmazonClientException ex) {
        System.out.println("Upload failed: " + ex.getMessage());
        return false;

    }
    return true;
}

From source file:com.amediamanager.service.VideoServiceImpl.java

License:Apache License

@Override
public Video save(String bucket, String videoKey) throws ParseException {

    // From bucket and key, get metadata from video that was just uploaded
    GetObjectMetadataRequest metadataReq = new GetObjectMetadataRequest(bucket, videoKey);
    ObjectMetadata metadata = s3Client.getObjectMetadata(metadataReq);
    Map<String, String> userMetadata = metadata.getUserMetadata();

    Video video = new Video();

    video.setDescription(userMetadata.get("description"));
    video.setOwner(userMetadata.get("owner"));
    video.setId(userMetadata.get("uuid"));
    video.setTitle(userMetadata.get("title"));
    video.setPrivacy(Privacy.fromName(userMetadata.get("privacy")));
    video.setCreatedDate(new SimpleDateFormat("MM/dd/yyyy").parse(userMetadata.get("createddate")));
    video.setOriginalKey(videoKey);//w  w  w.  j  a  v  a  2 s  . c om
    video.setBucket(userMetadata.get("bucket"));
    video.setUploadedDate(new Date());

    Set<Tag> tags = new HashSet<Tag>();
    for (String tag : userMetadata.get("tags").split(",")) {
        tags.add(new Tag(tag.trim()));
    }
    video.setTags(tags);

    save(video);

    return video;
}

From source file:com.clicktravel.infrastructure.persistence.aws.s3.S3FileStore.java

License:Apache License

/**
 * Returns a Map of the user meta-data associated with the given S3 Object
 *
 * This is a work-around for a bug in the AWS Java SDK which treats HTTP headers in a case-insensitive manner.
 *
 * @see <a href="https://github.com/aws/aws-sdk-java/pull/326">https://github.com/aws/aws-sdk-java/pull/326</a>
 *
 * @param s3Object The S3Object for which the user meta-data is to be obtained.
 * @return key-value map for user meta-data
 *///w w w.j a  v  a  2  s  . c om
private Map<String, String> getUserMetaData(final S3Object s3Object) {
    final ObjectMetadata objectMetaData = s3Object.getObjectMetadata();
    final Map<String, String> userMetaData = objectMetaData.getUserMetadata();
    if (userMetaData.isEmpty()) {
        for (final Entry<String, Object> entry : objectMetaData.getRawMetadata().entrySet()) {
            final String normalisedKey = entry.getKey().toLowerCase();
            if (normalisedKey.startsWith(USER_METADATA_HEADER_PREFIX)) {
                final String value = String.valueOf(entry.getValue());
                userMetaData.put(normalisedKey.substring(USER_METADATA_HEADER_PREFIX.length()), value);
            }
        }
    }
    return userMetaData;
}

From source file:com.emc.ecs.sync.model.object.S3SyncObject.java

License:Open Source License

protected SyncMetadata toSyncMeta(ObjectMetadata s3meta) {
    SyncMetadata meta = new SyncMetadata();

    meta.setCacheControl(s3meta.getCacheControl());
    meta.setContentDisposition(s3meta.getContentDisposition());
    meta.setContentEncoding(s3meta.getContentEncoding());
    if (s3meta.getContentMD5() != null)
        meta.setChecksum(new Checksum("MD5", s3meta.getContentMD5()));
    meta.setContentType(s3meta.getContentType());
    meta.setHttpExpires(s3meta.getHttpExpiresDate());
    meta.setExpirationDate(s3meta.getExpirationTime());
    meta.setModificationTime(s3meta.getLastModified());
    meta.setContentLength(s3meta.getContentLength());
    meta.setUserMetadata(toMetaMap(s3meta.getUserMetadata()));

    return meta;/*  w ww .  j  a  va2 s . com*/
}

From source file:com.emc.vipr.s3.sample._06_ReadObjectWithMetadata.java

License:Open Source License

public static void main(String[] args) throws Exception {
    // create the ViPR S3 Client
    ViPRS3Client s3 = ViPRS3Factory.getS3Client();

    // retrieve the object key from user
    System.out.println("Enter the object key:");
    String key = new BufferedReader(new InputStreamReader(System.in)).readLine();

    // read the specified object from the demo bucket
    S3Object object = s3.getObject(ViPRS3Factory.S3_BUCKET, key);

    // get the metadata for the object
    ObjectMetadata metadata = object.getObjectMetadata();

    // print out the object key/value and metadata for validation
    System.out.println(String.format("Metadata for [%s/%s]", ViPRS3Factory.S3_BUCKET, key));
    Map<String, String> metadataList = metadata.getUserMetadata();
    for (Map.Entry<String, String> entry : metadataList.entrySet()) {
        System.out.println(String.format("    %s = %s", entry.getKey(), entry.getValue()));
    }//from w w  w  .j  av  a 2s .c  o  m
}

From source file:com.eucalyptus.imaging.manifest.DownloadManifestFactory.java

License:Open Source License

private static boolean objectExist(@Nonnull EucaS3Client s3Client, String bucketName, String objectName)
        throws EucalyptusCloudException {
    try {//from  ww  w.ja  va2s .  c om
        ObjectMetadata metadata = s3Client.getS3Client().getObjectMetadata(bucketName, objectName);
        if (metadata == null || metadata.getUserMetadata() == null)
            return false;
        Map<String, String> userData = metadata.getUserMetadata();
        String expire = userData.get(MANIFEST_EXPIRATION);
        if (expire == null) {
            return false;
        } else {
            Long currentTime = (new Date()).getTime();
            Long expireTime = Long.parseLong(expire);
            return expireTime > currentTime;
        }
    } catch (Exception ex) {
        return false;
    }
}

From source file:com.eucalyptus.objectstorage.providers.s3.S3ProviderClient.java

License:Open Source License

protected void populateResponseMetadata(final ObjectStorageDataResponseType reply,
        final ObjectMetadata metadata) {
    reply.setSize(metadata.getContentLength());
    reply.setContentDisposition(metadata.getContentDisposition());
    reply.setContentType(metadata.getContentType());
    reply.setEtag(metadata.getETag());//from   ww w . j  a v  a2s  . co  m
    reply.setLastModified(metadata.getLastModified());

    if (metadata.getUserMetadata() != null && metadata.getUserMetadata().size() > 0) {
        if (reply.getMetaData() == null)
            reply.setMetaData(new ArrayList<MetaDataEntry>());

        for (String k : metadata.getUserMetadata().keySet()) {
            reply.getMetaData().add(new MetaDataEntry(k, metadata.getUserMetadata().get(k)));
        }
    }

}

From source file:com.facebook.presto.hive.s3.PrestoS3FileSystem.java

License:Apache License

private static long getObjectSize(Path path, ObjectMetadata metadata) throws IOException {
    Map<String, String> userMetadata = metadata.getUserMetadata();
    String length = userMetadata.get(UNENCRYPTED_CONTENT_LENGTH);
    if (userMetadata.containsKey(SERVER_SIDE_ENCRYPTION) && length == null) {
        throw new IOException(
                format("%s header is not set on an encrypted object: %s", UNENCRYPTED_CONTENT_LENGTH, path));
    }//w  w  w  .  j ava 2s . c  o  m
    return (length != null) ? Long.parseLong(length) : metadata.getContentLength();
}

From source file:com.jeet.s3.AmazonS3ClientWrapper.java

License:Open Source License

public Map getUserMetadata(String path) {
    try {/*from w  w w . j  a  va2s.  co m*/
        ObjectMetadata objectMetadata = s3Client.getObjectMetadata(Constants.BUCKET_NAME, path);
        return objectMetadata != null ? objectMetadata.getUserMetadata() : null;
    } catch (Exception e) {
        e.printStackTrace();
        return Collections.EMPTY_MAP;
    }
}