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

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

Introduction

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

Prototype

public void setContentLength(long contentLength) 

Source Link

Document

<p> Sets the Content-Length HTTP header indicating the size of the associated object in bytes.

Usage

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);/*ww  w .  j  a v a 2s . co 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.SnapshotObjectOps.java

License:Open Source License

public void uploadSnapshot(File snapshotFile, SnapshotProgressCallback callback, String snapshotKey,
        String snapshotId) throws EucalyptusCloudException {
    try {/*from  www.j av  a  2  s .  c o  m*/
        s3Client.getS3Client().listObjects(StorageProperties.SNAPSHOT_BUCKET);
    } catch (Exception ex) {
        try {
            //if (!s3Client.getS3Client().doesBucketExist(StorageProperties.SNAPSHOT_BUCKET)) {
            s3Client.getS3Client().createBucket(StorageProperties.SNAPSHOT_BUCKET);
            //}
        } catch (Exception e) {
            LOG.error("Snapshot upload failed. Unable to create bucket: snapshots", e);
            throw new EucalyptusCloudException(e);
        }
    }
    try {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(snapshotFile.length());
        //FIXME: need to set MD5
        s3Client.getS3Client().putObject(StorageProperties.SNAPSHOT_BUCKET, snapshotKey,
                new FileInputStreamWithCallback(snapshotFile, callback), metadata);
    } catch (Exception ex) {
        LOG.error("Snapshot " + snapshotId + " upload failed to: " + snapshotKey, ex);
        throw new EucalyptusCloudException(ex);
    }
}

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

License:Open Source License

protected ObjectMetadata getS3ObjectMetadata(PutObjectType request) {
    ObjectMetadata meta = new ObjectMetadata();
    if (request.getMetaData() != null) {
        for (MetaDataEntry m : request.getMetaData()) {
            meta.addUserMetadata(m.getName(), m.getValue());
        }/*from w  w w . ja  va2 s  .c o m*/
    }

    if (!Strings.isNullOrEmpty(request.getContentLength())) {
        meta.setContentLength(Long.parseLong(request.getContentLength()));
    }

    if (!Strings.isNullOrEmpty(request.getContentMD5())) {
        meta.setContentMD5(request.getContentMD5());
    }

    if (!Strings.isNullOrEmpty(request.getContentType())) {
        meta.setContentType(request.getContentType());
    }

    return meta;
}

From source file:com.formkiq.core.service.AssetServiceS3Default.java

License:Apache License

@Override
public void createFolder(final String folder) {

    // create meta-data for your folder and set content-length to 0
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(0);

    // create empty content
    InputStream emptyContent = new ByteArrayInputStream(new byte[0]);

    // create a PutObjectRequest passing the folder name suffixed by /
    PutObjectRequest putObjectRequest = new PutObjectRequest(this.s3BucketName, folder + "/", emptyContent,
            metadata);//from   ww w .ja v a 2 s.  c o  m

    // send request to S3 to create folder
    getS3Connection().putObject(putObjectRequest);
}

From source file:com.formkiq.core.service.AssetServiceS3Default.java

License:Apache License

@Override
public void saveAsset(final String folder, final String asset, final byte[] bytes) {

    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(bytes.length);
    metadata.setContentType("application/zip");

    try {// www .j av a2  s  .c o  m

        String filename = getFilename(folder, asset);

        AmazonS3 connection = getS3Connection();

        PutObjectRequest req = new PutObjectRequest(this.s3BucketName, filename, bis, metadata);

        connection.putObject(req);

        LOG.log(Level.FINE, "saving " + filename + " to " + S3_BUCKET_NAME);

    } finally {

        IOUtils.closeQuietly(bis);
    }
}

From source file:com.ge.predix.sample.blobstore.repository.BlobstoreService.java

License:Apache License

/**
 * Adds a new Blob to the binded bucket in the Object Store
 *
 * @param obj S3Object to be added/*from   ww w  .j  av  a  2  s .c  o m*/
 * @throws Exception
 */
public void put(S3Object obj) throws Exception {
    if (obj == null) {
        log.error("put(): Empty file provided");
        throw new Exception("File is null");
    }
    InputStream is = obj.getObjectContent();

    List<PartETag> partETags = new ArrayList<>();

    InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucket, obj.getKey());
    InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest);
    try {

        int i = 1;
        int currentPartSize = 0;
        ByteArrayOutputStream tempBuffer = new ByteArrayOutputStream();
        int byteValue;
        while ((byteValue = is.read()) != -1) {
            tempBuffer.write(byteValue);
            currentPartSize = tempBuffer.size();
            if (currentPartSize == (50 * 1024 * 1024)) //make this a const
            {
                byte[] b = tempBuffer.toByteArray();
                ByteArrayInputStream byteStream = new ByteArrayInputStream(b);

                UploadPartRequest uploadPartRequest = new UploadPartRequest().withBucketName(bucket)
                        .withKey(obj.getKey()).withUploadId(initResponse.getUploadId()).withPartNumber(i++)
                        .withInputStream(byteStream).withPartSize(currentPartSize);
                partETags.add(s3Client.uploadPart(uploadPartRequest).getPartETag());

                tempBuffer.reset();
            }
        }
        log.info("currentPartSize: " + currentPartSize);
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(currentPartSize);
        obj.setObjectMetadata(objectMetadata);

        if (i == 1 && currentPartSize < (5 * 1024 * 1024)) // make this a const
        {
            s3Client.abortMultipartUpload(
                    new AbortMultipartUploadRequest(bucket, obj.getKey(), initResponse.getUploadId()));

            byte[] b = tempBuffer.toByteArray();
            ByteArrayInputStream byteStream = new ByteArrayInputStream(b);
            objectMetadata.setContentType(getContentType(b));
            obj.setObjectMetadata(objectMetadata);

            PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, obj.getKey(), byteStream,
                    obj.getObjectMetadata());
            s3Client.putObject(putObjectRequest);
            return;
        }

        if (currentPartSize > 0 && currentPartSize <= (50 * 1024 * 1024)) // make this a const
        {
            byte[] b = tempBuffer.toByteArray();
            ByteArrayInputStream byteStream = new ByteArrayInputStream(b);

            log.info("currentPartSize: " + currentPartSize);
            log.info("byteArray: " + b);

            UploadPartRequest uploadPartRequest = new UploadPartRequest().withBucketName(bucket)
                    .withKey(obj.getKey()).withUploadId(initResponse.getUploadId()).withPartNumber(i)
                    .withInputStream(byteStream).withPartSize(currentPartSize);
            partETags.add(s3Client.uploadPart(uploadPartRequest).getPartETag());
        }
    } catch (Exception e) {
        log.error("put(): Exception occurred in put(): " + e.getMessage());
        s3Client.abortMultipartUpload(
                new AbortMultipartUploadRequest(bucket, obj.getKey(), initResponse.getUploadId()));
        throw e;
    }
    CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest()
            .withBucketName(bucket).withPartETags(partETags).withUploadId(initResponse.getUploadId())
            .withKey(obj.getKey());

    s3Client.completeMultipartUpload(completeMultipartUploadRequest);
}

From source file:com.ge.predix.solsvc.blobstore.bootstrap.BlobstoreClientImpl.java

License:Apache License

/**
 * Adds a new Blob to the binded bucket in the Object Store
 *
 * @param obj S3Object to be added/*from w  w w. j  ava  2 s  .  co  m*/
 */
@Override
public String saveBlob(S3Object obj) {
    if (obj == null) {
        this.log.error("put(): Empty file provided"); //$NON-NLS-1$
        throw new RuntimeException("File is null"); //$NON-NLS-1$
    }
    List<PartETag> partETags = new ArrayList<>();
    String bucket = this.blobstoreConfig.getBucketName();
    InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucket, obj.getKey());
    InitiateMultipartUploadResult initResponse = this.s3Client.initiateMultipartUpload(initRequest);
    try (InputStream is = obj.getObjectContent();) {

        int i = 1;
        int currentPartSize = 0;
        ByteArrayOutputStream tempBuffer = new ByteArrayOutputStream();
        int byteValue;
        while ((byteValue = is.read()) != -1) {
            tempBuffer.write(byteValue);
            currentPartSize = tempBuffer.size();
            if (currentPartSize == (50 * 1024 * 1024)) //make this a const
            {
                byte[] b = tempBuffer.toByteArray();
                ByteArrayInputStream byteStream = new ByteArrayInputStream(b);

                UploadPartRequest uploadPartRequest = new UploadPartRequest().withBucketName(bucket)
                        .withKey(obj.getKey()).withUploadId(initResponse.getUploadId()).withPartNumber(i++)
                        .withInputStream(byteStream).withPartSize(currentPartSize);
                partETags.add(this.s3Client.uploadPart(uploadPartRequest).getPartETag());

                tempBuffer.reset();
            }
        }
        this.log.info("currentPartSize: " + currentPartSize); //$NON-NLS-1$
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(currentPartSize);
        if (this.enableSSE) {
            objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
        }
        obj.setObjectMetadata(objectMetadata);

        if (i == 1 && currentPartSize < (5 * 1024 * 1024)) // make this a const
        {
            this.s3Client.abortMultipartUpload(
                    new AbortMultipartUploadRequest(bucket, obj.getKey(), initResponse.getUploadId()));

            byte[] b = tempBuffer.toByteArray();
            ByteArrayInputStream byteStream = new ByteArrayInputStream(b);
            objectMetadata.setContentType(getContentType(b));
            if (this.enableSSE) {
                objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
            }
            obj.setObjectMetadata(objectMetadata);

            PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, obj.getKey(), byteStream,
                    obj.getObjectMetadata());
            this.s3Client.putObject(putObjectRequest);

            ObjectMetadata meta = this.s3Client.getObjectMetadata(bucket, obj.getKey());
            Map<String, Object> headers = meta.getRawMetadata();
            for (Map.Entry<String, Object> entry : headers.entrySet()) {
                this.log.info("Object Metadata -- " + entry.getKey() + ": " + entry.getValue().toString()); //$NON-NLS-1$ //$NON-NLS-2$
            }

            return initResponse.getUploadId();
        }

        if (currentPartSize > 0 && currentPartSize <= (50 * 1024 * 1024)) // make this a const
        {
            byte[] b = tempBuffer.toByteArray();
            ByteArrayInputStream byteStream = new ByteArrayInputStream(b);

            this.log.info("currentPartSize: " + currentPartSize); //$NON-NLS-1$
            this.log.info("byteArray: " + b); //$NON-NLS-1$

            UploadPartRequest uploadPartRequest = new UploadPartRequest().withBucketName(bucket)
                    .withKey(obj.getKey()).withUploadId(initResponse.getUploadId()).withPartNumber(i)
                    .withInputStream(byteStream).withPartSize(currentPartSize);
            partETags.add(this.s3Client.uploadPart(uploadPartRequest).getPartETag());
        }

        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest()
                .withBucketName(bucket).withPartETags(partETags).withUploadId(initResponse.getUploadId())
                .withKey(obj.getKey());

        this.s3Client.completeMultipartUpload(completeMultipartUploadRequest);
        return initResponse.getUploadId();
    } catch (Exception e) {
        this.log.error("put(): Exception occurred in put(): " + e.getMessage()); //$NON-NLS-1$
        this.s3Client.abortMultipartUpload(
                new AbortMultipartUploadRequest(bucket, obj.getKey(), initResponse.getUploadId()));
        throw new RuntimeException("put(): Exception occurred in put(): ", e); //$NON-NLS-1$
    }
}

From source file:com.github.abhinavmishra14.aws.s3.service.impl.AwsS3IamServiceImpl.java

License:Open Source License

@Override
public PutObjectResult createDirectory(final String bucketName, final String dirName,
        final CannedAccessControlList cannedAcl) throws AmazonClientException, AmazonServiceException {
    LOGGER.info("createDirectory invoked, bucketName: {}, dirName: {} and cannedAccessControlList: {}",
            bucketName, dirName, cannedAcl);
    final ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(0);
    // Create empty content,since creating empty folder needs an empty content
    final InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
    // Create a PutObjectRequest passing the directory name suffixed by '/'
    final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,
            dirName + AWSUtilConstants.SEPARATOR, emptyContent, metadata).withCannedAcl(cannedAcl);
    return s3client.putObject(putObjectRequest);
}

From source file:com.github.abhinavmishra14.aws.s3.service.impl.AwsS3IamServiceImpl.java

License:Open Source License

@Override
public PutObjectResult createDirectory(final String bucketName, final String dirName,
        final boolean isPublicAccessible) throws AmazonClientException, AmazonServiceException {
    LOGGER.info("createDirectory invoked, bucketName: {}, dirName: {} and isPublicAccessible: {}", bucketName,
            dirName, isPublicAccessible);
    final ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(0);
    // Create empty content,since creating empty folder needs an empty content
    final InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
    // Create a PutObjectRequest passing the directory name suffixed by '/'
    final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,
            dirName + AWSUtilConstants.SEPARATOR, emptyContent, metadata);
    if (isPublicAccessible) {
        putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead);
    }/*from  w ww.j  a  v a 2s.com*/
    return s3client.putObject(putObjectRequest);
}

From source file:com.github.abhinavmishra14.aws.s3.service.impl.AwsS3IamServiceImpl.java

License:Open Source License

@Override
public boolean hasWritePermissionOnBucket(final String bucketName) {
    LOGGER.info("Checking bucket write permission..");
    boolean hasWritePermissions = false;
    final ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(0);
    // Create empty content
    final InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
    // Create a PutObjectRequest with test object
    final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,
            AWSUtilConstants.SAMPLE_FILE_NAME, emptyContent, metadata);
    try {// ww w . ja va  2  s .c  om
        if (s3client.putObject(putObjectRequest) != null) {
            LOGGER.info("Permissions validated!");
            //User has write permissions, TestPassed. 
            hasWritePermissions = true;
            //Delete the test object
            deleteObject(bucketName, AWSUtilConstants.SAMPLE_FILE_NAME);
        }
    } catch (AmazonClientException s3Ex) {
        LOGGER.warn("Write permissions not available!", s3Ex.getMessage());
    }
    return hasWritePermissions;
}