Search in sources :

Example 16 with ObjectMetadata

use of com.amazonaws.services.s3.model.ObjectMetadata in project YCSB by brianfrankcooper.

the class S3Client method readFromStorage.

/**
  * Download an object from S3.
  *
  * @param bucket
  *            The name of the bucket
  * @param key
  *            The file key of the object to upload/update.
  * @param result
  *            The Hash map where data from the object are written
  *
  */
protected Status readFromStorage(String bucket, String key, HashMap<String, ByteIterator> result, SSECustomerKey ssecLocal) {
    try {
        Map.Entry<S3Object, ObjectMetadata> objectAndMetadata = getS3ObjectAndMetadata(bucket, key, ssecLocal);
        //consuming the stream
        InputStream objectData = objectAndMetadata.getKey().getObjectContent();
        // writing the stream to bytes and to results
        int sizeOfFile = (int) objectAndMetadata.getValue().getContentLength();
        byte[] inputStreamToByte = new byte[sizeOfFile];
        objectData.read(inputStreamToByte, 0, sizeOfFile);
        result.put(key, new ByteArrayByteIterator(inputStreamToByte));
        objectData.close();
        objectAndMetadata.getKey().close();
    } catch (Exception e) {
        System.err.println("Not possible to get the object " + key);
        e.printStackTrace();
        return Status.ERROR;
    }
    return Status.OK;
}
Also used : ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) S3Object(com.amazonaws.services.s3.model.S3Object) HashMap(java.util.HashMap) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) DBException(com.yahoo.ycsb.DBException)

Example 17 with ObjectMetadata

use of com.amazonaws.services.s3.model.ObjectMetadata in project h2o-3 by h2oai.

the class PersistS3 method uriToKey.

@Override
public Key uriToKey(URI uri) throws IOException {
    AmazonS3 s3 = getClient();
    // Decompose URI into bucket, key
    String[] parts = decodePath(uri.toString());
    try {
        ObjectMetadata om = s3.getObjectMetadata(parts[0], parts[1]);
        // Voila: create S3 specific key pointing to the file
        return S3FileVec.make(encodePath(parts[0], parts[1]), om.getContentLength());
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().contains("404")) {
            throw new IOException(e);
        } else {
            Log.err("AWS failed for " + Arrays.toString(parts) + ": " + e.getMessage());
            throw e;
        }
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3)

Example 18 with ObjectMetadata

use of com.amazonaws.services.s3.model.ObjectMetadata in project gradle by gradle.

the class S3Client method put.

public void put(InputStream inputStream, Long contentLength, URI destination) {
    checkRequiredJigsawModuleIsOnPath();
    try {
        S3RegionalResource s3RegionalResource = new S3RegionalResource(destination);
        String bucketName = s3RegionalResource.getBucketName();
        String s3BucketKey = s3RegionalResource.getKey();
        configureClient(s3RegionalResource);
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(contentLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, s3BucketKey, inputStream, objectMetadata);
        LOGGER.debug("Attempting to put resource:[{}] into s3 bucket [{}]", s3BucketKey, bucketName);
        amazonS3Client.putObject(putObjectRequest);
    } catch (AmazonClientException e) {
        throw ResourceExceptions.putFailed(destination, e);
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 19 with ObjectMetadata

use of com.amazonaws.services.s3.model.ObjectMetadata in project alluxio by Alluxio.

the class S3AUnderFileSystem method createEmptyObject.

@Override
protected boolean createEmptyObject(String key) {
    try {
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(0);
        meta.setContentMD5(DIR_HASH);
        meta.setContentType(Mimetypes.MIMETYPE_OCTET_STREAM);
        mClient.putObject(new PutObjectRequest(mBucketName, key, new ByteArrayInputStream(new byte[0]), meta));
        return true;
    } catch (AmazonClientException e) {
        LOG.error("Failed to create object: {}", key, e);
        return false;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AmazonClientException(com.amazonaws.AmazonClientException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 20 with ObjectMetadata

use of com.amazonaws.services.s3.model.ObjectMetadata in project alluxio by Alluxio.

the class S3AOutputStream method close.

@Override
public void close() throws IOException {
    if (mClosed) {
        return;
    }
    mLocalOutputStream.close();
    String path = getUploadPath();
    try {
        // Generate the object metadata by setting server side encryption, md5 checksum, the file
        // length, and encoding as octet stream since no assumptions are made about the file type
        ObjectMetadata meta = new ObjectMetadata();
        if (SSE_ENABLED) {
            meta.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
        }
        if (mHash != null) {
            meta.setContentMD5(new String(Base64.encode(mHash.digest())));
        }
        meta.setContentLength(mFile.length());
        meta.setContentEncoding(Mimetypes.MIMETYPE_OCTET_STREAM);
        // Generate the put request and wait for the transfer manager to complete the upload, then
        // delete the temporary file on the local machine
        PutObjectRequest putReq = new PutObjectRequest(mBucketName, path, mFile).withMetadata(meta);
        mManager.upload(putReq).waitForUploadResult();
        if (!mFile.delete()) {
            LOG.error("Failed to delete temporary file @ {}", mFile.getPath());
        }
    } catch (Exception e) {
        LOG.error("Failed to upload {}. Temporary file @ {}", path, mFile.getPath());
        throw new IOException(e);
    }
    // Set the closed flag, close can be retried until mFile.delete is called successfully
    mClosed = true;
}
Also used : IOException(java.io.IOException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)64 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)20 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)18 AmazonServiceException (com.amazonaws.AmazonServiceException)17 AmazonClientException (com.amazonaws.AmazonClientException)15 IOException (java.io.IOException)13 CopyObjectRequest (com.amazonaws.services.s3.model.CopyObjectRequest)10 Copy (com.amazonaws.services.s3.transfer.Copy)8 Upload (com.amazonaws.services.s3.transfer.Upload)8 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)8 Date (java.util.Date)6 File (java.io.File)5 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)4 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)4 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)3 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)3 S3Object (com.amazonaws.services.s3.model.S3Object)3 InterruptedIOException (java.io.InterruptedIOException)3 ProgressEvent (com.amazonaws.event.ProgressEvent)2