Example usage for com.amazonaws.services.s3.model ObjectMetadata setContentLength

List of usage examples for com.amazonaws.services.s3.model ObjectMetadata setContentLength

Introduction

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

Prototype

public void setContentLength(long contentLength) 

Source Link

Document

<p> Sets the Content-Length HTTP header indicating the size of the associated object in bytes.

Usage

From source file:com.netflix.exhibitor.core.s3.S3Utils.java

License:Apache License

public static ObjectMetadata simpleUploadFile(S3Client client, byte[] bytes, String bucket, String key)
        throws Exception {
    byte[] md5 = md5(bytes, bytes.length);

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(bytes.length);
    metadata.setLastModified(new Date());
    metadata.setContentMD5(S3Utils.toBase64(md5));
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, new ByteArrayInputStream(bytes),
            metadata);/*w w  w. ja  va  2s .co m*/
    PutObjectResult putObjectResult = client.putObject(putObjectRequest);

    if (!putObjectResult.getETag().equals(S3Utils.toHex(md5))) {
        throw new Exception("Unable to match MD5 for config");
    }

    return metadata;
}

From source file:com.netflix.spinnaker.front50.model.S3StorageService.java

License:Apache License

@Override
public <T extends Timestamped> void storeObject(ObjectType objectType, String objectKey, T item) {
    if (readOnlyMode) {
        throw new ReadOnlyModeException();
    }//w ww. j  a  v a 2s  . c  om
    try {
        item.setLastModifiedBy(AuthenticatedRequest.getSpinnakerUser().orElse("anonymous"));
        byte[] bytes = objectMapper.writeValueAsBytes(item);

        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(bytes.length);
        objectMetadata.setContentMD5(
                new String(org.apache.commons.codec.binary.Base64.encodeBase64(DigestUtils.md5(bytes))));

        amazonS3.putObject(bucket, buildS3Key(objectType.group, objectKey, objectType.defaultMetadataFilename),
                new ByteArrayInputStream(bytes), objectMetadata);
        writeLastModified(objectType.group);
    } catch (JsonProcessingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.netflix.spinnaker.front50.model.S3StorageService.java

License:Apache License

private void writeLastModified(String group) {
    if (readOnlyMode) {
        throw new ReadOnlyModeException();
    }//from  ww w  .  j  a va 2 s  . com
    try {
        byte[] bytes = objectMapper
                .writeValueAsBytes(Collections.singletonMap("lastModified", System.currentTimeMillis()));

        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(bytes.length);
        objectMetadata.setContentMD5(
                new String(org.apache.commons.codec.binary.Base64.encodeBase64(DigestUtils.md5(bytes))));

        amazonS3.putObject(bucket, buildTypedFolder(rootFolder, group) + "/last-modified.json",
                new ByteArrayInputStream(bytes), objectMetadata);
    } catch (JsonProcessingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.netflix.spinnaker.front50.model.S3Support.java

License:Apache License

public void update(String id, T item) {
    try {/*from  w w  w.  j  a  v  a 2s.co m*/
        byte[] bytes = objectMapper.writeValueAsBytes(item);

        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(bytes.length);
        objectMetadata.setContentMD5(
                new String(org.apache.commons.codec.binary.Base64.encodeBase64(DigestUtils.md5(bytes))));

        amazonS3.putObject(bucket, buildS3Key(id), new ByteArrayInputStream(bytes), objectMetadata);
        writeLastModified();
    } catch (JsonProcessingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.netflix.spinnaker.front50.model.S3Support.java

License:Apache License

private void writeLastModified() {
    try {/* w  w w  .  jav  a2  s .  c  o  m*/
        byte[] bytes = objectMapper
                .writeValueAsBytes(Collections.singletonMap("lastModified", System.currentTimeMillis()));

        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(bytes.length);
        objectMetadata.setContentMD5(
                new String(org.apache.commons.codec.binary.Base64.encodeBase64(DigestUtils.md5(bytes))));

        amazonS3.putObject(bucket, rootFolder + "last-modified.json", new ByteArrayInputStream(bytes),
                objectMetadata);
    } catch (JsonProcessingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.nike.cerberus.service.S3StoreService.java

License:Apache License

public void put(String path, String value) {
    byte[] content;
    try {//from  w  ww  . j a  va  2 s.c om
        content = value.getBytes(ConfigConstants.DEFAULT_ENCODING);
    } catch (UnsupportedEncodingException e) {
        throw new UnexpectedDataEncodingException("Value to be stored has unexpected encoding.", e);
    }
    ByteArrayInputStream contentAsStream = new ByteArrayInputStream(content);
    ObjectMetadata md = new ObjectMetadata();
    md.setContentLength(content.length);
    s3Client.putObject(s3Bucket, getFullPath(path), contentAsStream, md);
}

From source file:com.openkm.util.backup.RepositoryS3Backup.java

License:Open Source License

/**
 * Performs a recursive repository content export with metadata
 *///w w w  . j  a v a  2  s  .c o  m
private static ImpExpStats backupHelper(String token, String fldPath, AmazonS3 s3, String bucket,
        boolean metadata, Writer out, InfoDecorator deco)
        throws FileNotFoundException, PathNotFoundException, AccessDeniedException, ParseException,
        NoSuchGroupException, RepositoryException, IOException, DatabaseException {
    log.info("backup({}, {}, {}, {}, {}, {})", new Object[] { token, fldPath, bucket, metadata, out, deco });
    ImpExpStats stats = new ImpExpStats();
    DocumentModule dm = ModuleManager.getDocumentModule();
    FolderModule fm = ModuleManager.getFolderModule();
    MetadataAdapter ma = MetadataAdapter.getInstance(token);
    Gson gson = new Gson();

    for (Iterator<Document> it = dm.getChildren(token, fldPath).iterator(); it.hasNext();) {
        File tmpDoc = null;
        InputStream is = null;
        FileOutputStream fos = null;
        boolean upload = true;

        try {
            Document docChild = it.next();
            String path = docChild.getPath().substring(1);
            ObjectMetadata objMeta = new ObjectMetadata();

            if (Config.REPOSITORY_CONTENT_CHECKSUM) {
                if (exists(s3, bucket, path)) {
                    objMeta = s3.getObjectMetadata(bucket, path);

                    if (docChild.getActualVersion().getChecksum().equals(objMeta.getETag())) {
                        upload = false;
                    }
                }
            }

            if (upload) {
                tmpDoc = FileUtils.createTempFileFromMime(docChild.getMimeType());
                fos = new FileOutputStream(tmpDoc);
                is = dm.getContent(token, docChild.getPath(), false);
                IOUtils.copy(is, fos);
                PutObjectRequest request = new PutObjectRequest(bucket, path, tmpDoc);

                if (metadata) {
                    // Metadata
                    DocumentMetadata dmd = ma.getMetadata(docChild);
                    String json = gson.toJson(dmd);
                    objMeta.addUserMetadata("okm", json);
                }

                request.setMetadata(objMeta);
                s3.putObject(request);
                out.write(deco.print(docChild.getPath(), docChild.getActualVersion().getSize(), null));
                out.flush();
            } else {
                if (metadata) {
                    // Metadata
                    DocumentMetadata dmd = ma.getMetadata(docChild);
                    String json = gson.toJson(dmd);
                    objMeta.addUserMetadata("okm", json);

                    // Update object metadata
                    CopyObjectRequest copyObjReq = new CopyObjectRequest(bucket, path, bucket, path);
                    copyObjReq.setNewObjectMetadata(objMeta);
                    s3.copyObject(copyObjReq);
                }

                log.info("Don't need to upload document {}", docChild.getPath());
            }

            // Stats
            stats.setSize(stats.getSize() + docChild.getActualVersion().getSize());
            stats.setDocuments(stats.getDocuments() + 1);
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(fos);
            FileUtils.deleteQuietly(tmpDoc);
        }
    }

    for (Iterator<Folder> it = fm.getChildren(token, fldPath).iterator(); it.hasNext();) {
        InputStream is = null;

        try {
            Folder fldChild = it.next();
            String path = fldChild.getPath().substring(1) + "/";
            is = new ByteArrayInputStream(new byte[0]);
            ObjectMetadata objMeta = new ObjectMetadata();
            objMeta.setContentLength(0);
            PutObjectRequest request = new PutObjectRequest(bucket, path, is, objMeta);

            // Metadata
            if (metadata) {
                FolderMetadata fmd = ma.getMetadata(fldChild);
                String json = gson.toJson(fmd);
                objMeta.addUserMetadata("okm", json);
            }

            request.setMetadata(objMeta);
            s3.putObject(request);

            ImpExpStats tmp = backupHelper(token, fldChild.getPath(), s3, bucket, metadata, out, deco);

            // Stats
            stats.setSize(stats.getSize() + tmp.getSize());
            stats.setDocuments(stats.getDocuments() + tmp.getDocuments());
            stats.setFolders(stats.getFolders() + tmp.getFolders() + 1);
            stats.setOk(stats.isOk() && tmp.isOk());
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    log.debug("backupHelper: {}", stats);
    return stats;
}

From source file:com.proofpoint.event.collector.combiner.S3CombineObjectMetadataStore.java

License:Apache License

private boolean writeMetadataFile(EventPartition eventPartition, CombinedGroup combinedGroup, String sizeName) {
    byte[] json = jsonCodec.toJson(combinedGroup).getBytes(Charsets.UTF_8);
    URI metadataFile = toMetadataLocation(eventPartition, sizeName);
    try {//from   w ww . j a v  a2 s .c om
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(json.length);
        metadata.setContentMD5(Base64.encodeBase64String(DigestUtils.md5(json)));
        metadata.setContentType(MediaType.APPLICATION_JSON);
        InputStream input = new ByteArrayInputStream(json);
        s3Service.putObject(getS3Bucket(metadataFile), getS3ObjectKey(metadataFile), input, metadata);
        return true;
    } catch (AmazonClientException e) {
        log.warn(e, "error writing metadata file: %s", metadataFile);
        return false;
    }
}

From source file:com.rathravane.clerk.impl.s3.S3IamDb.java

License:Apache License

void storeObject(String key, JSONObject o) throws IamSvcException {
    try {/*from ww w.  java2  s  .c o m*/
        fCache.put(key, JsonUtil.clone(o));

        final String data = o.toString();
        final InputStream is = new ByteArrayInputStream(data.getBytes("UTF-8"));
        final long length = data.length();

        final ObjectMetadata om = new ObjectMetadata();
        om.setContentLength(length);
        om.setContentType("application/json");
        fDb.putObject(new PutObjectRequest(fBucketId, key, is, om));
    } catch (AmazonS3Exception x) {
        throw new IamSvcException(x);
    } catch (UnsupportedEncodingException e) {
        throw new IamSvcException(e);
    }
}

From source file:com.sangupta.urn.service.impl.AmazonS3UrnStorageServiceImpl.java

License:Apache License

@Override
protected String save(UrnObject urnObject) {
    InputStream stream = new ByteArrayInputStream(urnObject.bytes);

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(urnObject.bytes.length);

    if (AssertUtils.isNotEmpty(urnObject.name)) {
        metadata.setContentDisposition("filename=" + urnObject.name);
    }//from  w w  w. j ava  2  s  . com

    if (AssertUtils.isNotEmpty(urnObject.mime)) {
        metadata.setContentType(urnObject.mime);
    }

    if (urnObject.expiry > 0) {
        metadata.setHttpExpiresDate(new Date(urnObject.expiry));
    }

    PutObjectResult result = this.client.putObject(this.bucketName, urnObject.key, stream, metadata);
    if (result == null) {
        return null;
    }

    return this.client.getResourceUrl(this.bucketName, urnObject.key);
}