Examples of ObjectMetadata


Examples of com.amazonaws.services.s3.model.ObjectMetadata

            throws AmazonClientException, AmazonServiceException {
        assertParameterNotNull(putObjectRequest, "The PutObjectRequest parameter must be specified when uploading an object");

        String bucketName = putObjectRequest.getBucketName();
        String key = putObjectRequest.getKey();
        ObjectMetadata metadata = putObjectRequest.getMetadata();
        InputStream input = putObjectRequest.getInputStream();
        if (metadata == null) metadata = new ObjectMetadata();

        assertParameterNotNull(bucketName, "The bucket name parameter must be specified when uploading an object");
        assertParameterNotNull(key, "The key parameter must be specified when uploading an object");

        // If a file is specified for upload, we need to pull some additional
        // information from it to auto-configure a few options
        if (putObjectRequest.getFile() != null) {
            File file = putObjectRequest.getFile();

            // Always set the content length, even if it's already set
            metadata.setContentLength(file.length());

            // Only set the content type if it hasn't already been set
            if (metadata.getContentType() == null) {
                metadata.setContentType(Mimetypes.getInstance().getMimetype(file));
            }

            try {
                byte[] md5Hash = ServiceUtils.computeMD5Hash(new FileInputStream(file));
                metadata.setContentMD5(ServiceUtils.toBase64(md5Hash));
            } catch (Exception e) {
                throw new AmazonClientException(
                        "Unable to calculate MD5 hash: " + e.getMessage(), e);
            }

            try {
                input = new RepeatableFileInputStream(file);
            } catch (FileNotFoundException fnfe) {
                throw new AmazonClientException("Unable to find file to upload", fnfe);
            }
        }

        Request<Void> request = createRequest(bucketName, key, putObjectRequest);

        if (putObjectRequest.getCannedAcl() != null) {
            request.addHeader(Headers.S3_CANNED_ACL, putObjectRequest.getCannedAcl().toString());
        }

        if (putObjectRequest.getStorageClass() != null) {
            request.addHeader(Headers.STORAGE_CLASS, putObjectRequest.getStorageClass());
        }

        if (metadata.getContentLength() <= 0) {
            /*
             * There's nothing we can do except for let the HTTP client buffer
             * the input stream contents if the caller doesn't tell us how much
             * data to expect in a stream since we have to explicitly tell
             * Amazon S3 how much we're sending before we start sending any of
             * it.
             */
            log.warn("No content length specified for stream data.  " +
                     "Stream contents will be buffered in memory and could result in " +
                     "out of memory errors.");
        }

        if (!input.markSupported()) {
            input = new RepeatableInputStream(input, Constants.DEFAULT_STREAM_BUFFER_SIZE);
        }

        if (metadata.getContentMD5() == null) {
            /*
             * If the user hasn't set the content MD5, then we don't want to
             * buffer the whole stream in memory just to calculate it. Instead,
             * we can calculate it on the fly and validate it with the returned
             * ETag from the object upload.
             */
            try {
                input = new MD5DigestCalculatingInputStream(input);
            } catch (NoSuchAlgorithmException e) {
                log.warn("No MD5 digest algorithm available.  Unable to calculate " +
                         "checksum and verify data integrity.", e);
            }
        }

        if (metadata.getContentType() == null) {
            /*
             * Default to the "application/octet-stream" if the user hasn't
             * specified a content type.
             */
            metadata.setContentType(Mimetypes.MIMETYPE_OCTET_STREAM);
        }

        populateRequestMetadata(request, metadata);
        signRequest(request, HttpMethodName.PUT, bucketName, key);
        HttpRequest httpRequest = convertToHttpRequest(request, HttpMethodName.PUT);
        httpRequest.setContent(input);

        ObjectMetadata returnedMetadata = null;
        try {
            S3MetadataResponseHandler responseHandler = new S3MetadataResponseHandler();
            returnedMetadata = (ObjectMetadata)client.execute(httpRequest, responseHandler, errorResponseHandler);
        } finally {
            try {input.close();} catch (Exception e) {
                log.warn("Unable to cleanly close input stream: " + e.getMessage(), e);
            }
        }

        String contentMd5 = metadata.getContentMD5();
        if (input instanceof MD5DigestCalculatingInputStream) {
            MD5DigestCalculatingInputStream md5DigestInputStream = (MD5DigestCalculatingInputStream)input;
            contentMd5 = ServiceUtils.toBase64(md5DigestInputStream.getMd5Digest());
        }

        if (returnedMetadata != null && contentMd5 != null) {
            byte[] clientSideHash = ServiceUtils.fromBase64(contentMd5);
            byte[] serverSideHash = ServiceUtils.fromHex(returnedMetadata.getETag());

            if (!Arrays.equals(clientSideHash, serverSideHash)) {
                throw new AmazonClientException("Unable to verify integrity of data upload.  " +
                        "Client calculated content hash didn't match hash calculated by Amazon S3.  " +
                        "You may need to delete the data stored in Amazon S3.");
            }
        }

        PutObjectResult result = new PutObjectResult();
        result.setETag(returnedMetadata.getETag());
        result.setVersionId(returnedMetadata.getVersionId());
        return result;
    }
View Full Code Here

Examples of com.amazonaws.services.s3.model.ObjectMetadata

        if (copyObjectRequest.getStorageClass() != null) {
            request.addHeader(Headers.STORAGE_CLASS, copyObjectRequest.getStorageClass());
        }

        ObjectMetadata newObjectMetadata = copyObjectRequest.getNewObjectMetadata();
        if (newObjectMetadata != null) {
            request.addHeader(Headers.METADATA_DIRECTIVE, "REPLACE");
            populateRequestMetadata(request, newObjectMetadata);
        }
    }
View Full Code Here

Examples of com.amazonaws.services.s3.model.ObjectMetadata

            throws AmazonServiceException, AmazonClientException {

            appendUserAgent(putObjectRequest, USER_AGENT);

            if (putObjectRequest.getMetadata() == null)
                putObjectRequest.setMetadata(new ObjectMetadata());
            ObjectMetadata metadata = putObjectRequest.getMetadata();

            if ( TransferManagerUtils.getRequestFile(putObjectRequest) != null ) {
                File file = TransferManagerUtils.getRequestFile(putObjectRequest);

                // Always set the content length, even if it's already set
                metadata.setContentLength(file.length());

                // Only set the content type if it hasn't already been set
                if ( metadata.getContentType() == null ) {
                    metadata.setContentType(Mimetypes.getInstance().getMimetype(file));
                }
            }

            String description = "Uploading to " + putObjectRequest.getBucketName() + "/" + putObjectRequest.getKey();
            TransferProgressImpl transferProgress = new TransferProgressImpl();
View Full Code Here

Examples of com.amazonaws.services.s3.model.ObjectMetadata

        // Add our own transfer progress listener
        TransferProgressImpl transferProgress = new TransferProgressImpl();
        ProgressListenerChain listenerChain = new ProgressListenerChain(new TransferProgressUpdatingListener(
                transferProgress), getObjectRequest.getGeneralProgressListener());
        getObjectRequest.setGeneralProgressListener(listenerChain);
        final ObjectMetadata objectMetadata = s3.getObjectMetadata(getObjectRequest.getBucketName(), getObjectRequest.getKey());

        final StartDownloadLock startDownloadLock = new StartDownloadLock();
        final DownloadImpl download = new DownloadImpl(description, transferProgress, listenerChain, null, stateListener);
        long contentLength = objectMetadata.getContentLength();
        if (getObjectRequest.getRange() != null && getObjectRequest.getRange().length == 2) {
            long startingByte = getObjectRequest.getRange()[0];
            long lastByte     = getObjectRequest.getRange()[1];
            contentLength     = lastByte - startingByte;
        }
View Full Code Here

Examples of com.amazonaws.services.s3.model.ObjectMetadata

            if (f.isFile()) {
                totalSize += f.length();
                String key = f.getAbsolutePath().substring(directory.getAbsolutePath().length() + 1)
                        .replaceAll("\\\\", "/");
               
                ObjectMetadata metadata=new ObjectMetadata();
               
                // Invoke the callback if it's present.
                // The callback allows the user to customize the metadata for each file being uploaded.
                if(metadataProvider!=null){
                    metadataProvider.provideObjectMetadata(f,metadata);
View Full Code Here

Examples of com.amazonaws.services.s3.model.ObjectMetadata

            final String fileContent = "testing put and delete";
            final InputStream inputStream = new ByteArrayInputStream(
                    fileContent.getBytes());
            final String key = UUID.randomUUID().toString() + ".txt";

            final ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(fileContent.length());

            client.putObject(bucketName, key, inputStream, metadata);
            client.deleteObject(bucketName, key);

            return true;
View Full Code Here

Examples of com.amazonaws.services.s3.model.ObjectMetadata

      String dataStoreRef = stripProtocol(ref.getDataStoreReference(), true);
      try {
        PutObjectRequest request = new PutObjectRequest(
            bucketName, dataStoreRef, new File(origRef));
        if (encrypt) {
          ObjectMetadata requestMetadata = new ObjectMetadata();
          requestMetadata.setServerSideEncryption(AES_256_SERVER_SIDE_ENCRYPTION);    
          request.setMetadata(requestMetadata);
        }
        s3Client.putObject(request);
      } catch (AmazonClientException e) {
        throw new DataTransferException(String.format(
View Full Code Here

Examples of com.amazonaws.services.s3.model.ObjectMetadata

    @Override public void writeBlob(final String blobName, final InputStream is, final long sizeInBytes, final WriterListener listener) {
        blobStore.executor().execute(new Runnable() {
            @Override public void run() {
                try {
                    ObjectMetadata md = new ObjectMetadata();
                    md.setContentLength(sizeInBytes);
                    PutObjectResult objectResult = blobStore.client().putObject(blobStore.bucket(), buildKey(blobName), is, md);
                    listener.onCompleted();
                } catch (Exception e) {
                    listener.onFailure(e);
                }
View Full Code Here

Examples of com.amazonaws.services.s3.model.ObjectMetadata

   * writes the value 'data' in the file 'id'
   */
  public String uploadData(String sid, String cid, byte[] data, String id) throws StorageCloudException {
    try {

      ObjectMetadata metadata = new ObjectMetadata();
      metadata.setContentLength(data.length);
      ByteArrayInputStream in = new ByteArrayInputStream(data);
      if(sid == null){
        conn.putObject(new PutObjectRequest(bucketName, id, in, metadata))
      }else{
        AccessControlList acl = new AccessControlList();
View Full Code Here

Examples of com.amazonaws.services.s3.model.ObjectMetadata

    }

    public Exchange createExchange(ExchangePattern pattern, S3Object s3Object) {
        LOG.trace("Getting object with key [{}] from bucket [{}]...", s3Object.getKey(), s3Object.getBucketName());
       
        ObjectMetadata objectMetadata = s3Object.getObjectMetadata();
       
        LOG.trace("Got object [{}]", s3Object);
       
        Exchange exchange = new DefaultExchange(this, pattern);
        Message message = exchange.getIn();
        message.setBody(s3Object.getObjectContent());
        message.setHeader(S3Constants.KEY, s3Object.getKey());
        message.setHeader(S3Constants.BUCKET_NAME, s3Object.getBucketName());
        message.setHeader(S3Constants.E_TAG, objectMetadata.getETag());
        message.setHeader(S3Constants.LAST_MODIFIED, objectMetadata.getLastModified());
        message.setHeader(S3Constants.VERSION_ID, objectMetadata.getVersionId());
        message.setHeader(S3Constants.CONTENT_TYPE, objectMetadata.getContentType());
        message.setHeader(S3Constants.CONTENT_MD5, objectMetadata.getContentMD5());
        message.setHeader(S3Constants.CONTENT_LENGTH, objectMetadata.getContentLength());
        message.setHeader(S3Constants.CONTENT_ENCODING, objectMetadata.getContentEncoding());
        message.setHeader(S3Constants.CONTENT_DISPOSITION, objectMetadata.getContentDisposition());
        message.setHeader(S3Constants.CACHE_CONTROL, objectMetadata.getCacheControl());
       
        return exchange;
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.