Search in sources :

Example 36 with ObjectMetadata

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

the class AwsS3DynamicSkinService method saveContentToAwsS3Bucket.

private void saveContentToAwsS3Bucket(final String objectKey, final String content, final DynamicSkinInstanceData data) {
    final InputStream inputStream = IOUtils.toInputStream(content);
    final ObjectMetadata objectMetadata = this.createObjectMetadata(content, data);
    final PutObjectRequest putObjectRequest = this.createPutObjectRequest(objectKey, inputStream, objectMetadata);
    log.info(ATTEMPTING_TO_SAVE_FILE_TO_AWS_S3_LOG_MSG, this.awsS3BucketConfig.getBucketName(), objectKey);
    this.saveContentToAwsS3Bucket(putObjectRequest);
    log.info(FILE_SAVED_TO_AWS_S3_LOG_MSG, this.awsS3BucketConfig.getBucketName(), objectKey);
}
Also used : InputStream(java.io.InputStream) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 37 with ObjectMetadata

use of com.amazonaws.services.s3.model.ObjectMetadata in project hippo by NHS-digital-website.

the class S3ConnectorImpl method uploadFile.

public S3ObjectMetadata uploadFile(InputStream fileStream, String fileName, String contentType) {
    String objectKey = s3ObjectKeyGenerator.generateObjectKey(fileName);
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentType(contentType);
    // initialise multipart upload
    InitiateMultipartUploadResult initResult = s3.initiateMultipartUpload(new InitiateMultipartUploadRequest(bucketName, objectKey, metadata));
    // loop parts
    List<PartETag> partETags;
    try {
        partETags = uploadParts(fileStream, bucketName, objectKey, initResult.getUploadId());
    } catch (Exception ex) {
        final String errorMessage = "Failed to upload file " + objectKey;
        log.error(errorMessage, ex);
        s3.abortMultipartUpload(new AbortMultipartUploadRequest(bucketName, objectKey, initResult.getUploadId()));
        throw new RuntimeException(errorMessage, ex);
    }
    // finalise multipart upload
    s3.completeMultipartUpload(new CompleteMultipartUploadRequest(bucketName, objectKey, initResult.getUploadId(), partETags));
    // The above put request returns metadata object but it's empty,
    // hence the need for a separate call to fetch actual metadata.
    ObjectMetadata resultMetadata = s3.getObjectMetadata(bucketName, objectKey);
    return new S3ObjectMetadataImpl(resultMetadata, bucketName, objectKey);
}
Also used : InitiateMultipartUploadResult(com.amazonaws.services.s3.model.InitiateMultipartUploadResult) InitiateMultipartUploadRequest(com.amazonaws.services.s3.model.InitiateMultipartUploadRequest) AbortMultipartUploadRequest(com.amazonaws.services.s3.model.AbortMultipartUploadRequest) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PartETag(com.amazonaws.services.s3.model.PartETag) IOException(java.io.IOException) CompleteMultipartUploadRequest(com.amazonaws.services.s3.model.CompleteMultipartUploadRequest)

Example 38 with ObjectMetadata

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

the class COSAPIClient method getFileStatusKeyBased.

private FileStatus getFileStatusKeyBased(String key, Path path) throws AmazonS3Exception {
    LOG.trace("internal method - get file status by key {}, path {}", key, path);
    FileStatus cachedFS = memoryCache.getFileStatus(path.toString());
    if (cachedFS != null) {
        return cachedFS;
    }
    ObjectMetadata meta = mClient.getObjectMetadata(mBucket, key);
    String sparkOrigin = meta.getUserMetaDataOf("data-origin");
    boolean stocatorCreated = false;
    if (sparkOrigin != null) {
        String tmp = (String) sparkOrigin;
        if (tmp.equals("stocator")) {
            stocatorCreated = true;
        }
    }
    mCachedSparkOriginated.put(key, Boolean.valueOf(stocatorCreated));
    FileStatus fs = createFileStatus(meta.getContentLength(), key, meta.getLastModified(), path);
    memoryCache.putFileStatus(path.toString(), fs);
    return fs;
}
Also used : FileStatus(org.apache.hadoop.fs.FileStatus) LocatedFileStatus(org.apache.hadoop.fs.LocatedFileStatus) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 39 with ObjectMetadata

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

the class COSAPIClient method isSparkOrigin.

/**
 * Checks if container/object exists and verifies that it contains
 * Data-Origin=stocator metadata If so, object was created by Spark.
 *
 * @param objectKey the key of the object
 * @param path the object path
 * @return boolean if object was created by Spark
 */
private boolean isSparkOrigin(String objectKey, String path) {
    LOG.debug("check spark origin for {}", objectKey);
    if (!objectKey.endsWith("/")) {
        LOG.debug("Key {} has no slash. Return false", objectKey);
        return false;
    } else {
        objectKey = objectKey.substring(0, objectKey.length() - 1);
    }
    if (mCachedSparkOriginated.containsKey(objectKey)) {
        boolean res = mCachedSparkOriginated.get(objectKey).booleanValue();
        LOG.debug("found cached for spark origin for {}. Status {}", objectKey, res);
        return res;
    }
    String key = getRealKey(objectKey);
    Boolean sparkOriginated = Boolean.FALSE;
    ObjectMetadata objMetadata = getObjectMetadata(key);
    if (objMetadata != null) {
        Object sparkOrigin = objMetadata.getUserMetaDataOf("data-origin");
        if (sparkOrigin != null) {
            String tmp = (String) sparkOrigin;
            if (tmp.equals("stocator")) {
                sparkOriginated = Boolean.TRUE;
            }
        }
    }
    mCachedSparkOriginated.put(key, sparkOriginated);
    LOG.debug("spark origin for {} is {} non cached", objectKey, sparkOriginated.booleanValue());
    return sparkOriginated.booleanValue();
}
Also used : ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 40 with ObjectMetadata

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

the class COSAPIClient method isJobSuccessful.

/**
 * Checks if container/object contains container/object/_SUCCESS If so, this
 * object was created by successful Hadoop job
 *
 * @param objectKey
 * @return boolean if job is successful
 */
private boolean isJobSuccessful(String objectKey) {
    LOG.trace("isJobSuccessful: for {}", objectKey);
    if (mCachedSparkJobsStatus.containsKey(objectKey)) {
        LOG.trace("isJobSuccessful: {} found cached", objectKey);
        return mCachedSparkJobsStatus.get(objectKey).booleanValue();
    }
    String key = getRealKey(objectKey);
    Path p = new Path(key, HADOOP_SUCCESS);
    ObjectMetadata statusMetadata = getObjectMetadata(p.toString());
    Boolean isJobOK = Boolean.FALSE;
    if (statusMetadata != null) {
        isJobOK = Boolean.TRUE;
    }
    LOG.debug("isJobSuccessful: not cached {}. Status is {}", objectKey, isJobOK);
    mCachedSparkJobsStatus.put(objectKey, isJobOK);
    return isJobOK.booleanValue();
}
Also used : StocatorPath(com.ibm.stocator.fs.common.StocatorPath) Path(org.apache.hadoop.fs.Path) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Aggregations

ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)75 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)26 IOException (java.io.IOException)19 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)18 AmazonClientException (com.amazonaws.AmazonClientException)17 AmazonServiceException (com.amazonaws.AmazonServiceException)17 CopyObjectRequest (com.amazonaws.services.s3.model.CopyObjectRequest)10 Upload (com.amazonaws.services.s3.transfer.Upload)10 ByteArrayInputStream (java.io.ByteArrayInputStream)10 InputStream (java.io.InputStream)9 Copy (com.amazonaws.services.s3.transfer.Copy)8 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)7 File (java.io.File)5 InterruptedIOException (java.io.InterruptedIOException)5 Date (java.util.Date)5 Path (org.apache.hadoop.fs.Path)5 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)4 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)3 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)3 InitiateMultipartUploadRequest (com.amazonaws.services.s3.model.InitiateMultipartUploadRequest)3