Java Amazon S3 writeStreamToS3File(AWSCredentials credentials, String bucketName, String key, ByteArrayOutputStream stream, String contentType, CannedAccessControlList acl)

Here you can find the source of writeStreamToS3File(AWSCredentials credentials, String bucketName, String key, ByteArrayOutputStream stream, String contentType, CannedAccessControlList acl)

Description

writeStreamToS3File - Write a given Stream to a specified S3 file, returing a pre-signed URL to the file.

License

Apache License

Parameter

Parameter Description
credentials - Credentials for accessing AWS Resources
bucketName - The name of the bucket to write to.
key - The key for the file to write.
stream - The stream to be written to the file.
contentType - Content type for the file.
acl - Access Control List specifying file permissions.

Return

- A pre-signed URL to access the file contents - with a 15 minute expiration time.

Declaration

public static String writeStreamToS3File(AWSCredentials credentials, String bucketName, String key,
        ByteArrayOutputStream stream, String contentType, CannedAccessControlList acl) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import com.amazonaws.HttpMethod;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;

import com.amazonaws.services.s3.model.ObjectMetadata;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Date;

public class Main {
    /**//from   www  .j ava  2 s . co m
     * writeStreamToS3File - Write a given Stream to a specified S3 file, returing a pre-signed URL to the file.  
     * 
      * @param credentials   - Credentials for accessing AWS Resources
      * @param bucketName   - The name of the bucket to write to.
      * @param key         - The key for the file to write.
      * @param stream      - The stream to be written to the file.
      * @param contentType   - Content type for the file.
      * @param acl         - Access Control List specifying file permissions.
      * @return            - A pre-signed URL to access the file contents - with a 15 minute expiration time.
      */
    public static String writeStreamToS3File(AWSCredentials credentials, String bucketName, String key,
            ByteArrayOutputStream stream, String contentType, CannedAccessControlList acl) {
        String result = "";
        AmazonS3Client conn = new AmazonS3Client(credentials);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType(contentType);
        metadata.setContentLength((long) stream.toByteArray().length);
        ByteArrayInputStream input = new ByteArrayInputStream(stream.toByteArray());
        conn.putObject(bucketName, key, (InputStream) input, metadata);
        conn.setObjectAcl(bucketName, key, acl);
        result = conn.generatePresignedUrl(bucketName, key, new Date(new Date().getTime() + 900000), HttpMethod.GET)
                .toString();
        return result;
    }
}