Search in sources :

Example 41 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project xin-open-enterprise-platform by hui0xin.

the class S3FileUploadService method createObjectMetadata.

/**
 * @param uploadMetadata
 * @return
 */
private ObjectMetadata createObjectMetadata(final FileUploadMetadata uploadMetadata) {
    if (uploadMetadata == null) {
        return null;
    }
    final ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(uploadMetadata.getContentLength());
    objectMetadata.setContentType(uploadMetadata.getContentType());
    objectMetadata.setCacheControl("max-age=" + this.properties.getCacheControlMaxAge());
    return objectMetadata;
}
Also used : ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 42 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project xin-open-enterprise-platform by hui0xin.

the class S3FileUploadService method uploadFile.

@Override
public FileUploadVo uploadFile(final String fileName, final File file, final FileUploadMetadata uploadMetadata) {
    final String key = this.getKey(fileName);
    final ObjectMetadata objectMetadata = this.createObjectMetadata(uploadMetadata);
    this.s3Client.putObject(new PutObjectRequest(this.properties.getBucketName(), key, file).withMetadata(objectMetadata));
    return FileUploadVo.builder().fileName(fileName).key(key).url(this.getSignedUrl(fileName)).build();
}
Also used : ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 43 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project pipes by pipecraft.

the class S3Bucket method getObjectMetadata.

@Override
public S3ObjectSummary getObjectMetadata(String key) throws IOException {
    if (isFolderPath(key)) {
        throw new FileNotFoundException("File not found: '" + getBucketName() + "/" + key + "'");
    }
    try {
        ObjectMetadata meta = s3.getObjectMetadata(getBucketName(), key);
        S3ObjectSummary res = new S3ObjectSummary();
        res.setBucketName(getBucketName());
        res.setKey(key);
        res.setETag(meta.getETag());
        res.setLastModified(meta.getLastModified());
        res.setSize(meta.getContentLength());
        res.setStorageClass(meta.getStorageClass());
        return res;
    } catch (AmazonClientException ace) {
        throw mapToIOException(extractAmazonServiceException(ace), "Failed getting metadata of '" + getBucketName() + "/" + key + "'");
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) FileNotFoundException(java.io.FileNotFoundException) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 44 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project extension-s3 by lucee.

the class S3 method setMetaData.

public void setMetaData(String bucketName, String objectName, Struct metadata) throws PageException, S3Exception {
    Decision dec = CFMLEngineFactory.getInstance().getDecisionUtil();
    Cast cas = CFMLEngineFactory.getInstance().getCastUtil();
    Iterator<Entry<Key, Object>> it = metadata.entryIterator();
    Entry<Key, Object> e;
    ObjectMetadata metadataCopy = new ObjectMetadata();
    Map<String, String> data = new ConcurrentHashMap<String, String>();
    while (it.hasNext()) {
        e = it.next();
        metadataCopy.addUserMetadata(toMetaDataKey(e.getKey()), cas.toString(e.getValue()));
    }
    bucketName = improveBucketName(bucketName);
    objectName = improveObjectName(objectName);
    if (!Util.isEmpty(objectName)) {
        AmazonS3Client client = getAmazonS3(bucketName, null);
        try {
            S3BucketWrapper bw = get(bucketName);
            if (bw == null)
                throw new S3Exception("there is no bucket [" + bucketName + "]");
            Bucket b = bw.getBucket();
            CopyObjectRequest request = new CopyObjectRequest(bucketName, objectName, bucketName, objectName).withNewObjectMetadata(metadataCopy);
            client.copyObject(request);
            flushExists(bucketName, objectName);
        } finally {
            client.release();
        }
    } else
        // TOOD possible?
        throw new S3Exception("cannot set metadata for a bucket");
}
Also used : Cast(lucee.runtime.util.Cast) S3BucketWrapper(org.lucee.extension.resource.s3.info.S3BucketWrapper) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) Decision(lucee.runtime.util.Decision) Entry(java.util.Map.Entry) AmazonS3Client(org.lucee.extension.resource.s3.pool.AmazonS3Client) CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) Bucket(com.amazonaws.services.s3.model.Bucket) ParentObject(org.lucee.extension.resource.s3.info.ParentObject) S3Object(com.amazonaws.services.s3.model.S3Object) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Key(lucee.runtime.type.Collection.Key)

Example 45 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project extension-s3 by lucee.

the class S3 method write.

/**
 * @param bucketName target bucket to create if not already exists
 * @param objectName object to create
 * @param data object content
 * @param acl access control list
 * @param region if bucket is not already existing, it get created using that region, if it is not
 *            defined the default region defined with the constructor is used
 * @throws S3Exception
 */
public void write(String bucketName, String objectName, String data, String mimeType, Charset charset, Object acl, String region) throws IOException {
    bucketName = improveBucketName(bucketName);
    objectName = improveObjectName(objectName, false);
    flushExists(bucketName, objectName);
    AmazonS3Client client = getAmazonS3(bucketName, region);
    String ct = toContentType(mimeType, charset, null);
    byte[] bytes = charset == null ? data.getBytes() : data.getBytes(charset);
    // unlikely this ever happen, so we do not write extra code for this
    if (data.length() > maxSize) {
        File tmp = File.createTempFile("writeString-", ".txt");
        try {
            Util.copy(new ByteArrayInputStream(bytes), new FileOutputStream(tmp), true, true);
            write(bucketName, objectName, tmp, acl, region);
            return;
        } finally {
            tmp.delete();
        }
    } else {
        ObjectMetadata md = new ObjectMetadata();
        if (ct != null)
            md.setContentType(ct);
        md.setLastModified(new Date());
        // create a PutObjectRequest passing the folder name suffixed by /
        md.setContentLength(bytes.length);
        PutObjectRequest por = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(bytes), md);
        if (acl != null)
            setACL(por, acl);
        try {
            // send request to S3 to create folder
            try {
                client.putObject(por);
                flushExists(bucketName, objectName);
            } catch (AmazonServiceException ase) {
                if (ase.getErrorCode().equals("EntityTooLarge")) {
                    S3Exception s3e = toS3Exception(ase);
                    if (s3e.getProposedSize() != 0 && s3e.getProposedSize() < maxSize) {
                        maxSize = s3e.getProposedSize();
                        write(bucketName, objectName, data, mimeType, charset, acl, region);
                        return;
                    }
                    throw s3e;
                }
                if (ase.getErrorCode().equals("NoSuchBucket")) {
                    createDirectory(bucketName, acl, region);
                    write(bucketName, objectName, data, mimeType, charset, acl, region);
                    return;
                } else
                    throw toS3Exception(ase);
            }
        } catch (AmazonServiceException se) {
            throw toS3Exception(se);
        } finally {
            client.release();
        }
    }
}
Also used : AmazonS3Client(org.lucee.extension.resource.s3.pool.AmazonS3Client) ByteArrayInputStream(java.io.ByteArrayInputStream) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) FileOutputStream(java.io.FileOutputStream) AmazonServiceException(com.amazonaws.AmazonServiceException) File(java.io.File) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Date(java.util.Date) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Aggregations

ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)566 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)191 ByteArrayInputStream (java.io.ByteArrayInputStream)157 Test (org.junit.Test)143 IOException (java.io.IOException)101 InputStream (java.io.InputStream)80 File (java.io.File)62 AmazonClientException (com.amazonaws.AmazonClientException)61 AmazonServiceException (com.amazonaws.AmazonServiceException)61 S3Object (com.amazonaws.services.s3.model.S3Object)59 AmazonS3 (com.amazonaws.services.s3.AmazonS3)54 Date (java.util.Date)46 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)34 GetObjectMetadataRequest (com.amazonaws.services.s3.model.GetObjectMetadataRequest)33 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)32 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)30 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)29 Upload (com.amazonaws.services.s3.transfer.Upload)26 SdkClientException (com.amazonaws.SdkClientException)24 CopyObjectRequest (com.amazonaws.services.s3.model.CopyObjectRequest)24