Search in sources :

Example 46 with ObjectMetadata

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

the class S3 method write.

public void write(String bucketName, String objectName, Resource res, Object acl, String region) throws IOException {
    if (res instanceof File) {
        write(bucketName, objectName, (File) res, acl, region);
        return;
    }
    String ct = CFMLEngineFactory.getInstance().getResourceUtil().getMimeType(res, null);
    try {
        bucketName = improveBucketName(bucketName);
        objectName = improveObjectName(objectName, false);
        flushExists(bucketName, objectName);
        // send request to S3 to create folder
        AmazonS3Client client = getAmazonS3(bucketName, region);
        if (res.length() > maxSize) {
            File tmp = File.createTempFile("writeResource-", ".txt");
            try {
                Util.copy(res.getInputStream(), new FileOutputStream(tmp), true, true);
                write(bucketName, objectName, tmp, acl, region);
                return;
            } finally {
                tmp.delete();
            }
        } else {
            InputStream is = null;
            ObjectMetadata md = new ObjectMetadata();
            md.setLastModified(new Date());
            md.setContentLength(res.length());
            try {
                PutObjectRequest por = new PutObjectRequest(bucketName, objectName, is = res.getInputStream(), md);
                if (acl != null)
                    setACL(por, acl);
                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, res, acl, region);
                        return;
                    }
                    throw s3e;
                }
                if (ase.getErrorCode().equals("NoSuchBucket")) {
                    createDirectory(bucketName, acl, region);
                    write(bucketName, objectName, res, acl, region);
                    return;
                } else
                    throw toS3Exception(ase);
            } finally {
                Util.closeEL(is);
                client.release();
            }
        }
    } catch (AmazonServiceException se) {
        throw toS3Exception(se);
    }
}
Also used : AmazonS3Client(org.lucee.extension.resource.s3.pool.AmazonS3Client) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) 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)

Example 47 with ObjectMetadata

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

the class S3 method getMetaDataStruct.

public Struct getMetaDataStruct(String bucketName, String objectName) throws S3Exception {
    Struct sct = CFMLEngineFactory.getInstance().getCreationUtil().createStruct();
    bucketName = improveBucketName(bucketName);
    objectName = improveObjectName(objectName);
    sct.setEL("bucketName", bucketName);
    AmazonS3Client client = getAmazonS3(bucketName, null);
    try {
        if (Util.isEmpty(objectName)) {
        // TODO
        }
        if (objectName != null) {
            sct.setEL("objectName", objectName);
            S3Object o = client.getObject(bucketName, objectName);
            ObjectMetadata md = o.getObjectMetadata();
            Map<String, Object> rmd = md.getRawMetadata();
            Iterator<Entry<String, Object>> it = rmd.entrySet().iterator();
            Entry<String, Object> e;
            while (it.hasNext()) {
                e = it.next();
                sct.setEL(e.getKey().replace('-', '_'), e.getValue());
            }
            sct.setEL("lastModified", md.getLastModified());
        }
    } finally {
        client.release();
    }
    /*
		 * if (owner != null) { sct.put("owner", owner.getDisplayName()); sct.put("owner_id",
		 * owner.getId()); }
		 */
    return sct;
}
Also used : AmazonS3Client(org.lucee.extension.resource.s3.pool.AmazonS3Client) Entry(java.util.Map.Entry) ParentObject(org.lucee.extension.resource.s3.info.ParentObject) S3Object(com.amazonaws.services.s3.model.S3Object) S3Object(com.amazonaws.services.s3.model.S3Object) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Struct(lucee.runtime.type.Struct)

Example 48 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project aws-athena-query-federation by awslabs.

the class S3BlockSpiller method write.

/**
 * Writes (aka spills) a Block.
 */
protected SpillLocation write(Block block) {
    try {
        S3SpillLocation spillLocation = makeSpillLocation();
        EncryptionKey encryptionKey = spillConfig.getEncryptionKey();
        logger.info("write: Started encrypting block for write to {}", spillLocation);
        byte[] bytes = blockCrypto.encrypt(encryptionKey, block);
        totalBytesSpilled.addAndGet(bytes.length);
        logger.info("write: Started spilling block of size {} bytes", bytes.length);
        amazonS3.putObject(spillLocation.getBucket(), spillLocation.getKey(), new ByteArrayInputStream(bytes), new ObjectMetadata());
        logger.info("write: Completed spilling block of size {} bytes", bytes.length);
        return spillLocation;
    } catch (RuntimeException ex) {
        asyncException.compareAndSet(null, ex);
        logger.warn("write: Encountered error while writing block.", ex);
        throw ex;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) S3SpillLocation(com.amazonaws.athena.connector.lambda.domain.spill.S3SpillLocation) EncryptionKey(com.amazonaws.athena.connector.lambda.security.EncryptionKey) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 49 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project Project-Decode by cheery72.

the class UserServiceImpl method AwsFile.

private String AwsFile(MultipartFile file) {
    String fileName = createFileName(file.getOriginalFilename());
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(file.getSize());
    objectMetadata.setContentType(file.getContentType());
    try (InputStream inputStream = file.getInputStream()) {
        amazonS3.putObject(new PutObjectRequest(bucket, fileName, inputStream, objectMetadata).withCannedAcl(CannedAccessControlList.PublicRead));
    } catch (IOException e) {
        throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "파일 업로드에 실패했습니다.");
    }
    return String.format(url + "/%s", fileName);
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) ResponseStatusException(org.springframework.web.server.ResponseStatusException) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 50 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project aws-lambda-developer-guide by awsdocs.

the class Handler method handleRequest.

@Override
public String handleRequest(S3Event s3event, Context context) {
    try {
        logger.info("EVENT: " + gson.toJson(s3event));
        S3EventNotificationRecord record = s3event.getRecords().get(0);
        String srcBucket = record.getS3().getBucket().getName();
        // Object key may have spaces or unicode non-ASCII characters.
        String srcKey = record.getS3().getObject().getUrlDecodedKey();
        String dstBucket = srcBucket;
        String dstKey = "resized-" + srcKey;
        // Infer the image type.
        Matcher matcher = Pattern.compile(".*\\.([^\\.]*)").matcher(srcKey);
        if (!matcher.matches()) {
            logger.info("Unable to infer image type for key " + srcKey);
            return "";
        }
        String imageType = matcher.group(1);
        if (!(JPG_TYPE.equals(imageType)) && !(PNG_TYPE.equals(imageType))) {
            logger.info("Skipping non-image " + srcKey);
            return "";
        }
        // Download the image from S3 into a stream
        AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
        S3Object s3Object = s3Client.getObject(new GetObjectRequest(srcBucket, srcKey));
        InputStream objectData = s3Object.getObjectContent();
        // Read the source image
        BufferedImage srcImage = ImageIO.read(objectData);
        int srcHeight = srcImage.getHeight();
        int srcWidth = srcImage.getWidth();
        // Infer the scaling factor to avoid stretching the image
        // unnaturally
        float scalingFactor = Math.min(MAX_WIDTH / srcWidth, MAX_HEIGHT / srcHeight);
        int width = (int) (scalingFactor * srcWidth);
        int height = (int) (scalingFactor * srcHeight);
        BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = resizedImage.createGraphics();
        // Fill with white before applying semi-transparent (alpha) images
        g.setPaint(Color.white);
        g.fillRect(0, 0, width, height);
        // Simple bilinear resize
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.drawImage(srcImage, 0, 0, width, height, null);
        g.dispose();
        // Re-encode image to target format
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(resizedImage, imageType, os);
        InputStream is = new ByteArrayInputStream(os.toByteArray());
        // Set Content-Length and Content-Type
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(os.size());
        if (JPG_TYPE.equals(imageType)) {
            meta.setContentType(JPG_MIME);
        }
        if (PNG_TYPE.equals(imageType)) {
            meta.setContentType(PNG_MIME);
        }
        // Uploading to S3 destination bucket
        logger.info("Writing to: " + dstBucket + "/" + dstKey);
        try {
            s3Client.putObject(dstBucket, dstKey, is, meta);
        } catch (AmazonServiceException e) {
            logger.error(e.getErrorMessage());
            System.exit(1);
        }
        logger.info("Successfully resized " + srcBucket + "/" + srcKey + " and uploaded to " + dstBucket + "/" + dstKey);
        return "Ok";
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) ByteArrayInputStream(java.io.ByteArrayInputStream) S3EventNotificationRecord(com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord) AmazonServiceException(com.amazonaws.AmazonServiceException) S3Object(com.amazonaws.services.s3.model.S3Object) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

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