Example usage for com.amazonaws.services.s3.model PutObjectRequest putCustomRequestHeader

List of usage examples for com.amazonaws.services.s3.model PutObjectRequest putCustomRequestHeader

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model PutObjectRequest putCustomRequestHeader.

Prototype

public String putCustomRequestHeader(String name, String value) 

Source Link

Document

Put a new custom header to the map of custom header names to custom header values, and return the previous value if the header has already been set in this map.

Usage

From source file:org.alanwilliamson.amazon.s3.BackgroundUploader.java

License:Open Source License

private void uploadFile(Map<String, Object> jobFile) {

    File localFile = new File((String) jobFile.get("localpath"));
    if (!localFile.isFile()) {
        removeJobFile(jobFile);//from w w w. ja  va  2s. c om
        callbackCfc(jobFile, false, "local file no longer exists");
        cfEngine.log("AmazonS3Write.BackgroundUploader: file no longer exists=" + localFile.getName());
        return;
    }

    // Setup the object data
    ObjectMetadata omd = new ObjectMetadata();
    if (jobFile.containsKey("metadata"))
        omd.setUserMetadata((Map<String, String>) jobFile.get("metadata"));

    TransferManager tm = null;
    AmazonS3 s3Client = null;
    try {
        AmazonKey amazonKey = (AmazonKey) jobFile.get("amazonkey");
        s3Client = new AmazonBase().getAmazonS3(amazonKey);

        PutObjectRequest por = new PutObjectRequest((String) jobFile.get("bucket"), (String) jobFile.get("key"),
                localFile);
        por.setMetadata(omd);
        por.setStorageClass((StorageClass) jobFile.get("storage"));

        if (jobFile.containsKey("acl"))
            por.setCannedAcl(amazonKey.getAmazonCannedAcl((String) jobFile.get("acl")));

        if (jobFile.containsKey("aes256key"))
            por.setSSECustomerKey(new SSECustomerKey((String) jobFile.get("aes256key")));

        if (jobFile.containsKey("customheaders")) {
            Map<String, String> customheaders = (Map) jobFile.get("customheaders");

            Iterator<String> it = customheaders.keySet().iterator();
            while (it.hasNext()) {
                String k = it.next();
                por.putCustomRequestHeader(k, customheaders.get(k));
            }
        }

        long startTime = System.currentTimeMillis();
        tm = new TransferManager(s3Client);
        Upload upload = tm.upload(por);
        upload.waitForCompletion();

        log(jobFile, "Uploaded; timems=" + (System.currentTimeMillis() - startTime));

        removeJobFile(jobFile);
        callbackCfc(jobFile, true, null);

        if ((Boolean) jobFile.get("deletefile"))
            localFile.delete();

    } catch (Exception e) {
        log(jobFile, "Failed=" + e.getMessage());

        callbackCfc(jobFile, false, e.getMessage());

        int retry = (Integer) jobFile.get("retry");
        int attempt = (Integer) jobFile.get("attempt") + 1;

        if (retry == attempt) {
            removeJobFile(jobFile);
        } else {
            jobFile.put("attempt", attempt);
            jobFile.put("attemptdate", System.currentTimeMillis() + (Long) jobFile.get("retryms"));
            acceptFile(jobFile);
        }

        if (s3Client != null)
            cleanupMultiPartUploads(s3Client, (String) jobFile.get("bucket"));

    } finally {
        if (tm != null)
            tm.shutdownNow(true);
    }

}

From source file:org.alanwilliamson.amazon.s3.Write.java

License:Open Source License

private void writeData(AmazonKey amazonKey, String bucket, String key, Map<String, String> metadata,
        StorageClass storage, String mimetype, cfData data, int retry, int retryseconds, String acl,
        String aes256key, Map<String, String> customheaders) throws Exception {
    if (mimetype == null) {
        if (data.getDataType() == cfData.CFBINARYDATA)
            mimetype = "application/unknown";
        else if (cfData.isSimpleValue(data))
            mimetype = "text/plain";
        else/*from   www . j  a va2s  .com*/
            mimetype = "application/json";

        // Check to see if the mime type is in the metadata
        if (metadata != null && metadata.containsKey("Content-Type"))
            mimetype = metadata.get("Content-Type");
    }

    InputStream ios = null;
    long size = 0;
    if (data.getDataType() == cfData.CFSTRINGDATA) {
        ios = new java.io.ByteArrayInputStream(data.getString().getBytes());
        size = data.getString().length();
    } else if (data.getDataType() == cfData.CFBINARYDATA) {
        ios = new java.io.ByteArrayInputStream(((cfBinaryData) data).getByteArray());
        size = ((cfBinaryData) data).getLength();
    } else {
        serializejson json = new serializejson();
        StringBuilder out = new StringBuilder();
        json.encodeJSON(out, data, false, CaseType.MAINTAIN, DateType.LONG);
        size = out.length();
        mimetype = "application/json";
        ios = new java.io.ByteArrayInputStream(out.toString().getBytes());
    }

    // Setup the object data
    ObjectMetadata omd = new ObjectMetadata();
    if (metadata != null)
        omd.setUserMetadata(metadata);

    omd.setContentType(mimetype);
    omd.setContentLength(size);

    AmazonS3 s3Client = getAmazonS3(amazonKey);

    // Let us run around the number of attempts
    int attempts = 0;
    while (attempts < retry) {
        try {

            PutObjectRequest por = new PutObjectRequest(bucket, key, ios, omd);
            por.setStorageClass(storage);

            if (aes256key != null && !aes256key.isEmpty())
                por.setSSECustomerKey(new SSECustomerKey(aes256key));

            if (acl != null && !acl.isEmpty())
                por.setCannedAcl(amazonKey.getAmazonCannedAcl(acl));

            if (customheaders != null && !customheaders.isEmpty()) {
                Iterator<String> it = customheaders.keySet().iterator();
                while (it.hasNext()) {
                    String k = it.next();
                    por.putCustomRequestHeader(k, customheaders.get(k));
                }
            }

            s3Client.putObject(por);
            break;

        } catch (Exception e) {
            cfEngine.log("Failed: AmazonS3Write(bucket=" + bucket + "; key=" + key + "; attempt="
                    + (attempts + 1) + "; exception=" + e.getMessage() + ")");
            attempts++;

            if (attempts == retry)
                throw e;
            else
                Thread.sleep(retryseconds * 1000);
        }
    }
}

From source file:org.alanwilliamson.amazon.s3.Write.java

License:Open Source License

private void writeFile(AmazonKey amazonKey, String bucket, String key, Map<String, String> metadata,
        StorageClass storage, String localpath, int retry, int retryseconds, boolean deletefile,
        boolean background, String callback, String callbackdata, String appname, String acl, String aes256key,
        Map<String, String> customheaders) throws Exception {
    File localFile = new File(localpath);
    if (!localFile.isFile())
        throw new Exception("The file specified does not exist: " + localpath);

    // Push this to the background loader to handle and return immediately
    if (background) {
        BackgroundUploader.acceptFile(amazonKey, bucket, key, metadata, storage, localpath, retry, retryseconds,
                deletefile, callback, callbackdata, appname, acl, aes256key, customheaders);
        return;/*from   ww w.j  ava 2  s. c om*/
    }

    // Setup the object data
    ObjectMetadata omd = new ObjectMetadata();
    if (metadata != null)
        omd.setUserMetadata(metadata);

    AmazonS3 s3Client = getAmazonS3(amazonKey);

    // Let us run around the number of attempts
    int attempts = 0;
    while (attempts < retry) {
        try {

            PutObjectRequest por = new PutObjectRequest(bucket, key, localFile);
            por.setMetadata(omd);
            por.setStorageClass(storage);

            if (acl != null && !acl.isEmpty())
                por.setCannedAcl(amazonKey.getAmazonCannedAcl(acl));

            if (aes256key != null && !aes256key.isEmpty())
                por.setSSECustomerKey(new SSECustomerKey(aes256key));

            if (customheaders != null && !customheaders.isEmpty()) {
                Iterator<String> it = customheaders.keySet().iterator();
                while (it.hasNext()) {
                    String k = it.next();
                    por.putCustomRequestHeader(k, customheaders.get(k));
                }
            }

            s3Client.putObject(por);
            break;

        } catch (Exception e) {
            cfEngine.log("Failed: AmazonS3Write(bucket=" + bucket + "key=" + key + "; file=" + localFile
                    + "; attempt=" + (attempts + 1) + "; exception=" + e.getMessage() + ")");
            attempts++;

            if (attempts == retry)
                throw e;
            else
                Thread.sleep(retryseconds * 1000);
        }
    }

    // delete the file now that it is a success
    if (deletefile)
        localFile.delete();
}