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

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

Introduction

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

Prototype

public void setUserMetadata(Map<String, String> userMetadata) 

Source Link

Document

Sets the custom user-metadata for the associated object.

Usage

From source file:com.emc.ecs.sync.util.AwsS3Util.java

License:Open Source License

public static ObjectMetadata s3MetaFromSyncMeta(SyncMetadata syncMeta) {
    ObjectMetadata om = new ObjectMetadata();
    if (syncMeta.getCacheControl() != null)
        om.setCacheControl(syncMeta.getCacheControl());
    if (syncMeta.getContentDisposition() != null)
        om.setContentDisposition(syncMeta.getContentDisposition());
    if (syncMeta.getContentEncoding() != null)
        om.setContentEncoding(syncMeta.getContentEncoding());
    om.setContentLength(syncMeta.getContentLength());
    if (syncMeta.getChecksum() != null && syncMeta.getChecksum().getAlgorithm().equals("MD5"))
        om.setContentMD5(syncMeta.getChecksum().getValue());
    if (syncMeta.getContentType() != null)
        om.setContentType(syncMeta.getContentType());
    if (syncMeta.getHttpExpires() != null)
        om.setHttpExpiresDate(syncMeta.getHttpExpires());
    om.setUserMetadata(formatUserMetadata(syncMeta));
    if (syncMeta.getModificationTime() != null)
        om.setLastModified(syncMeta.getModificationTime());
    return om;//from w ww . j av  a2  s.co m
}

From source file:com.eucalyptus.blockstorage.S3SnapshotTransfer.java

License:Open Source License

private PutObjectResult uploadSnapshotAsSingleObject(String compressedSnapFileName, Long actualSize,
        Long uncompressedSize, SnapshotProgressCallback callback) throws Exception {
    callback.setUploadSize(actualSize);//  w ww.ja va2s.  c  o m
    FileInputStreamWithCallback snapInputStream = new FileInputStreamWithCallback(
            new File(compressedSnapFileName), callback);
    ObjectMetadata objectMetadata = new ObjectMetadata();
    Map<String, String> userMetadataMap = new HashMap<String, String>();
    userMetadataMap.put(UNCOMPRESSED_SIZE_KEY, String.valueOf(uncompressedSize)); // Send the uncompressed length as the metadata
    objectMetadata.setUserMetadata(userMetadataMap);
    objectMetadata.setContentLength(actualSize);

    return retryAfterRefresh(new Function<PutObjectRequest, PutObjectResult>() {

        @Override
        @Nullable
        public PutObjectResult apply(@Nullable PutObjectRequest arg0) {
            eucaS3Client.refreshEndpoint();
            return eucaS3Client.putObject(arg0);
        }

    }, new PutObjectRequest(bucketName, keyName, snapInputStream, objectMetadata), REFRESH_TOKEN_RETRIES);
}

From source file:com.eucalyptus.blockstorage.S3SnapshotTransfer.java

License:Open Source License

private String initiateMulitpartUpload(Long uncompressedSize) throws SnapshotInitializeMpuException {
    InitiateMultipartUploadResult initResponse = null;
    InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, keyName);
    ObjectMetadata objectMetadata = new ObjectMetadata();
    Map<String, String> userMetadataMap = new HashMap<String, String>();
    userMetadataMap.put(UNCOMPRESSED_SIZE_KEY, String.valueOf(uncompressedSize)); // Send the uncompressed length as the metadata
    objectMetadata.setUserMetadata(userMetadataMap);
    initRequest.setObjectMetadata(objectMetadata);

    try {/*from ww w.  j  a v a  2  s.c  o m*/
        LOG.info("Inititating multipart upload: snapshotId=" + snapshotId + ", bucketName=" + bucketName
                + ", keyName=" + keyName);
        initResponse = retryAfterRefresh(
                new Function<InitiateMultipartUploadRequest, InitiateMultipartUploadResult>() {

                    @Override
                    @Nullable
                    public InitiateMultipartUploadResult apply(@Nullable InitiateMultipartUploadRequest arg0) {
                        eucaS3Client.refreshEndpoint();
                        return eucaS3Client.initiateMultipartUpload(arg0);
                    }

                }, initRequest, REFRESH_TOKEN_RETRIES);
    } catch (Exception ex) {
        throw new SnapshotInitializeMpuException("Failed to initialize multipart upload part for snapshotId="
                + snapshotId + ", bucketName=" + bucketName + ", keyName=" + keyName, ex);
    }

    if (StringUtils.isBlank(initResponse.getUploadId())) {
        throw new SnapshotInitializeMpuException("Invalid upload ID for multipart upload part for snapshotId="
                + snapshotId + ", bucketName=" + bucketName + ", keyName=" + keyName);
    }
    return initResponse.getUploadId();
}

From source file:com.github.rholder.esthree.command.Put.java

License:Apache License

@Override
public Integer call() throws Exception {
    TransferManager t = new TransferManager(amazonS3Client);

    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setUserMetadata(metadata);

    Upload u = t.upload(new PutObjectRequest(bucket, key, inputFile).withMetadata(objectMetadata));

    // TODO this listener spews out garbage >100% on a retry, add a test to verify
    if (progressListener != null) {
        progressListener.withTransferProgress(new TransferProgressWrapper(u.getProgress()));
        u.addProgressListener(progressListener);
    }/*from  www  .ja  va 2s . c o m*/
    try {
        u.waitForCompletion();
    } finally {
        t.shutdownNow();
    }
    return 0;
}

From source file:com.ibm.stocator.fs.cos.COSAPIClient.java

License:Apache License

@Override
public FSDataOutputStream createObject(String objName, String contentType, Map<String, String> metadata,
        Statistics statistics) throws IOException {
    try {/*from   w  ww. ja v a  2s.c o  m*/
        String objNameWithoutBuket = objName;
        if (objName.startsWith(mBucket + "/")) {
            objNameWithoutBuket = objName.substring(mBucket.length() + 1);
        }
        if (blockUploadEnabled) {
            return new FSDataOutputStream(new COSBlockOutputStream(this, objNameWithoutBuket,
                    new SemaphoredDelegatingExecutor(threadPoolExecutor, blockOutputActiveBlocks, true),
                    partSize, blockFactory, contentType, new WriteOperationHelper(objNameWithoutBuket),
                    metadata), null);
        }

        if (!contentType.equals(Constants.APPLICATION_DIRECTORY)) {
            return new FSDataOutputStream(
                    new COSOutputStream(mBucket, objName, mClient, contentType, metadata, transfers, this),
                    statistics);
        } else {
            final InputStream im = new InputStream() {
                @Override
                public int read() throws IOException {
                    return -1;
                }
            };
            final ObjectMetadata om = new ObjectMetadata();
            om.setContentLength(0L);
            om.setContentType(contentType);
            om.setUserMetadata(metadata);
            // Remove the bucket name prefix from key path
            if (objName.startsWith(mBucket + "/")) {
                objName = objName.substring(mBucket.length() + 1);
            }
            /*
            if (!objName.endsWith("/")) {
              objName = objName + "/";
            }*/
            LOG.debug("bucket: {}, key {}", mBucket, objName);
            PutObjectRequest putObjectRequest = new PutObjectRequest(mBucket, objName, im, om);
            Upload upload = transfers.upload(putObjectRequest);
            upload.waitForUploadResult();
            OutputStream fakeStream = new OutputStream() {

                @Override
                public void write(int b) throws IOException {
                }

                @Override
                public void close() throws IOException {
                    super.close();
                }
            };
            return new FSDataOutputStream(fakeStream, statistics);
        }
    } catch (InterruptedException e) {
        throw new InterruptedIOException("Interrupted creating " + objName);
    } catch (IOException e) {
        LOG.error(e.getMessage());
        throw e;
    }
}

From source file:com.ibm.stocator.fs.cos.COSBlockOutputStream.java

License:Apache License

/**
 * Upload the current block as a single PUT request; if the buffer is empty a
 * 0-byte PUT will be invoked, as it is needed to create an entry at the far
 * end./*w  w w . j a v  a  2s. co  m*/
 *
 * @throws IOException any problem
 */
private void putObject() throws IOException {
    LOG.debug("Executing regular upload for {}", writeOperationHelper);

    final COSDataBlocks.DataBlock block = getActiveBlock();
    int size = block.dataSize();
    final COSDataBlocks.BlockUploadData uploadData = block.startUpload();
    final PutObjectRequest putObjectRequest = uploadData.hasFile()
            ? writeOperationHelper.newPutRequest(uploadData.getFile())
            : writeOperationHelper.newPutRequest(uploadData.getUploadStream(), size);

    final ObjectMetadata om = new ObjectMetadata();
    om.setUserMetadata(mMetadata);
    if (contentType != null && !contentType.isEmpty()) {
        om.setContentType(contentType);
    } else {
        om.setContentType("application/octet-stream");
    }
    putObjectRequest.setMetadata(om);
    ListenableFuture<PutObjectResult> putObjectResult = executorService.submit(new Callable<PutObjectResult>() {
        @Override
        public PutObjectResult call() throws Exception {
            PutObjectResult result;
            try {
                // the putObject call automatically closes the input
                // stream afterwards.
                result = writeOperationHelper.putObject(putObjectRequest);
            } finally {
                closeAll(LOG, uploadData, block);
            }
            return result;
        }
    });
    clearActiveBlock();
    // wait for completion
    try {
        putObjectResult.get();
    } catch (InterruptedException ie) {
        LOG.warn("Interrupted object upload", ie);
        Thread.currentThread().interrupt();
    } catch (ExecutionException ee) {
        throw extractException("regular upload", key, ee);
    }
}

From source file:com.ibm.stocator.fs.cos.COSOutputStream.java

License:Apache License

@Override
public void close() throws IOException {
    if (closed.getAndSet(true)) {
        return;//  w ww  . j a v  a2  s .  c  om
    }
    mBackupOutputStream.close();
    LOG.debug("OutputStream for key '{}' closed. Now beginning upload", mKey);
    try {
        final ObjectMetadata om = new ObjectMetadata();
        om.setContentLength(mBackupFile.length());
        om.setContentType(mContentType);
        om.setUserMetadata(mMetadata);

        PutObjectRequest putObjectRequest = new PutObjectRequest(mBucketName, mKey, mBackupFile);
        putObjectRequest.setMetadata(om);

        Upload upload = transfers.upload(putObjectRequest);

        upload.waitForUploadResult();
    } catch (InterruptedException e) {
        throw (InterruptedIOException) new InterruptedIOException(e.toString()).initCause(e);
    } catch (AmazonClientException e) {
        throw new IOException(String.format("saving output %s %s", mKey, e));
    } finally {
        if (!mBackupFile.delete()) {
            LOG.warn("Could not delete temporary cos file: {}", mBackupOutputStream);
        }
        super.close();
    }
    LOG.debug("OutputStream for key '{}' upload complete", mKey);
}

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

License:Open Source License

public boolean uploadFile(String path, InputStream is, String hash, Long fileLength) {
    boolean isFileUploaded = false;
    try {//from w w w  .j  a  v  a2 s  . co m
        ObjectMetadata objectMetadata = new ObjectMetadata();
        Map userMetadata = new HashMap();
        userMetadata.put(Constants.FILE_LENGTH_KEY, fileLength.toString());
        userMetadata.put(Constants.HASH_KEY, hash);
        userMetadata.put(Constants.LAST_MODIFIED_KEY, String.valueOf(new Date().getTime()));
        objectMetadata.setUserMetadata(userMetadata);
        PutObjectResult objectResult = s3Client
                .putObject(new PutObjectRequest(Constants.BUCKET_NAME, path, is, objectMetadata));
        if (!StringUtils.isEmpty(objectResult.getContentMd5())) {
            isFileUploaded = true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return isFileUploaded;
}

From source file:com.netflix.exhibitor.core.backup.s3.MockS3Client.java

License:Apache License

@Override
public synchronized PutObjectResult putObject(PutObjectRequest request) throws Exception {
    Map<String, String> userData = Maps.newHashMap();
    userData.put(BYTES_HEADER, Integer.toString(uploadedBytes.size()));

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteStreams.copy(request.getInputStream(), out);
    byte[] bytes = out.toByteArray();
    uploadedBytes.add(bytes);//w w  w.j  a  v a 2  s. c  o m

    byte[] md5bytes = S3Utils.md5(bytes, out.size());

    S3Object object = new S3Object();
    object.setKey(request.getKey());
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(bytes.length);
    metadata.setUserMetadata(userData);
    object.setObjectMetadata(metadata);
    uploads.put(request.getKey(), object);

    PutObjectResult result = new PutObjectResult();
    result.setETag(S3Utils.toHex(md5bytes));
    return result;
}

From source file:com.tvarit.plugin.TvaritTomcatDeployerMojo.java

License:Open Source License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {

    final MavenProject project = (MavenProject) this.getPluginContext().getOrDefault("project", null);
    if (templateUrl == null)
        try {//from  w  w w  .  j a va2  s .c  om
            templateUrl = new TemplateUrlMaker().makeUrl(project, "newinstance.template").toString();
        } catch (MalformedURLException e) {
            throw new MojoExecutionException(
                    "Could not create default url for templates. Please open an issue on github.", e);
        }

    final BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonS3Client s3Client = new AmazonS3Client(awsCredentials);
    final File warFile = project.getArtifact().getFile();
    final String key = "deployables/" + project.getGroupId() + "/" + project.getArtifactId() + "/"
            + project.getVersion() + "/" + warFile.getName();
    final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, warFile);
    final ObjectMetadata metadata = new ObjectMetadata();
    final HashMap<String, String> userMetadata = new HashMap<>();
    userMetadata.put("project_name", projectName);
    userMetadata.put("stack_template_url", templateUrl);
    userMetadata.put("private_key_name", sshKeyName);
    metadata.setUserMetadata(userMetadata);
    putObjectRequest.withMetadata(metadata);
    final PutObjectResult putObjectResult = s3Client.putObject(putObjectRequest);

    /*
            AmazonCloudFormationClient amazonCloudFormationClient = new AmazonCloudFormationClient(awsCredentials);
            final com.amazonaws.services.cloudformation.model.Parameter projectNameParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("projectName").withParameterValue(this.projectName);
            final com.amazonaws.services.cloudformation.model.Parameter publicSubnetsParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("publicSubnets").withParameterValue(commaSeparatedSubnetIds);
            final com.amazonaws.services.cloudformation.model.Parameter tvaritRoleParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("tvaritRole").withParameterValue(tvaritRole);
            final com.amazonaws.services.cloudformation.model.Parameter tvaritInstanceProfileParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("tvaritInstanceProfile").withParameterValue(this.tvaritInstanceProfile);
            final com.amazonaws.services.cloudformation.model.Parameter tvaritBucketNameParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("bucketName").withParameterValue(this.bucketName);
            final com.amazonaws.services.cloudformation.model.Parameter instanceSecurityGroupIdParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("sgId").withParameterValue(this.instanceSecurityGroupId);
            final com.amazonaws.services.cloudformation.model.Parameter sshKeyNameParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("keyName").withParameterValue(this.sshKeyName);
            final String warFileUrl = s3Client.getUrl(bucketName, key).toString();
            final com.amazonaws.services.cloudformation.model.Parameter warFileUrlParameter = new com.amazonaws.services.cloudformation.model.Parameter().withParameterKey("warFileUrl").withParameterValue(warFileUrl);
            final CreateStackRequest createStackRequest = new CreateStackRequest();
            if (templateUrl == null) {
    try {
        templateUrl = new TemplateUrlMaker().makeUrl(project, "newinstance.template").toString();
    } catch (MalformedURLException e) {
        throw new MojoExecutionException("Could not create default url for templates. Please open an issue on github.", e);
    }
            }
            createStackRequest.
        withStackName(projectName + "-instance-" + project.getVersion().replace(".", "-")).
        withParameters(
                projectNameParameter,
                publicSubnetsParameter,
                tvaritInstanceProfileParameter,
                tvaritRoleParameter,
                tvaritBucketNameParameter,
                instanceSecurityGroupIdParameter,
                warFileUrlParameter,
                sshKeyNameParameter
        ).
        withDisableRollback(true).
        withTemplateURL(templateUrl);
            createStackRequest.withDisableRollback(true);
            final Stack stack = new StackMaker().makeStack(createStackRequest, amazonCloudFormationClient, getLog());
            AmazonAutoScalingClient amazonAutoScalingClient = new AmazonAutoScalingClient(awsCredentials);
            final AttachInstancesRequest attachInstancesRequest = new AttachInstancesRequest();
            attachInstancesRequest.withInstanceIds(stack.getOutputs().get(0).getOutputValue(), stack.getOutputs().get(1).getOutputValue()).withAutoScalingGroupName(autoScalingGroupName);
            amazonAutoScalingClient.attachInstances(attachInstancesRequest);
    */

}