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

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

Introduction

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

Prototype

public ObjectMetadata() 

Source Link

Usage

From source file:org.geoserver.taskmanager.external.impl.S3FileServiceImpl.java

License:Open Source License

@Override
public void create(String filePath, InputStream content) throws IOException {
    // Check parameters
    if (content == null) {
        throw new IllegalArgumentException("Content of a file can not be null.");
    }//from   w  ww . j  a  v  a  2 s .c o m
    if (filePath == null) {
        throw new IllegalArgumentException("Name of a file can not be null.");
    }

    if (checkFileExists(filePath)) {
        throw new IllegalArgumentException("The file already exists");
    }
    File scratchFile = File.createTempFile("prefix", String.valueOf(System.currentTimeMillis()));
    try {
        if (!getS3Client().doesBucketExist(rootFolder)) {
            getS3Client().createBucket(rootFolder);
        }

        FileUtils.copyInputStreamToFile(content, scratchFile);

        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentEncoding(ENCODING);

        PutObjectRequest putObjectRequest = new PutObjectRequest(rootFolder, filePath, scratchFile);

        putObjectRequest.withMetadata(metadata);

        getS3Client().putObject(putObjectRequest);
    } catch (AmazonClientException e) {
        throw new IOException(e);
    } finally {
        if (scratchFile.exists()) {
            scratchFile.delete();
        }
    }
}

From source file:org.geowebcache.s3.S3BlobStore.java

License:Open Source License

@Override
public void put(TileObject obj) throws StorageException {
    final Resource blob = obj.getBlob();
    checkNotNull(blob);/*from w  ww .j  a v  a 2s .c o m*/
    checkNotNull(obj.getBlobFormat());

    final String key = keyBuilder.forTile(obj);
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(blob.getSize());

    String blobFormat = obj.getBlobFormat();
    String mimeType;
    try {
        mimeType = MimeType.createFromFormat(blobFormat).getMimeType();
    } catch (MimeException me) {
        throw Throwables.propagate(me);
    }
    objectMetadata.setContentType(mimeType);

    // don't bother for the extra call if there are no listeners
    final boolean existed;
    ObjectMetadata oldObj;
    if (listeners.isEmpty()) {
        existed = false;
        oldObj = null;
    } else {
        oldObj = s3Ops.getObjectMetadata(key);
        existed = oldObj != null;
    }

    final ByteArrayInputStream input = toByteArray(blob);
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, input, objectMetadata)
            .withCannedAcl(CannedAccessControlList.PublicRead);

    log.trace(log.isTraceEnabled() ? ("Storing " + key) : "");
    s3Ops.putObject(putObjectRequest);

    putParametersMetadata(obj.getLayerName(), obj.getParametersId(), obj.getParameters());

    /*
     * This is important because listeners may be tracking tile existence
     */
    if (!listeners.isEmpty()) {
        if (existed) {
            long oldSize = oldObj.getContentLength();
            listeners.sendTileUpdated(obj, oldSize);
        } else {
            listeners.sendTileStored(obj);
        }
    }
}

From source file:org.geowebcache.s3.S3Ops.java

License:Open Source License

public void putProperties(String resourceKey, Properties properties) throws StorageException {

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {/*from   w  w  w. ja  v a2  s .c  o  m*/
        properties.store(out, "");
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }

    byte[] bytes = out.toByteArray();
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(bytes.length);
    objectMetadata.setContentType("text/plain");

    InputStream in = new ByteArrayInputStream(bytes);
    PutObjectRequest putReq = new PutObjectRequest(bucketName, resourceKey, in, objectMetadata);
    putObject(putReq);
}

From source file:org.gradle.internal.resource.transport.aws.s3.S3Client.java

License:Apache License

public void put(InputStream inputStream, Long contentLength, URI destination) {
    try {/*from   w  w  w .  j  a va 2  s. c om*/
        S3RegionalResource s3RegionalResource = new S3RegionalResource(destination);
        String bucketName = s3RegionalResource.getBucketName();
        String s3BucketKey = s3RegionalResource.getKey();
        configureClient(s3RegionalResource);

        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(contentLength);

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, s3BucketKey, inputStream,
                objectMetadata);
        LOGGER.debug("Attempting to put resource:[{}] into s3 bucket [{}]", s3BucketKey, bucketName);

        amazonS3Client.putObject(putObjectRequest);
    } catch (AmazonClientException e) {
        throw ResourceExceptions.putFailed(destination, e);
    }
}

From source file:org.gridgain.grid.spi.checkpoint.s3.GridS3CheckpointSpi.java

License:Open Source License

/**
 * Writes given checkpoint data to a given S3 bucket. Data is serialized to
 * the binary stream and saved to the S3.
 *
 * @param data Checkpoint data./*from www .j a va 2s.  c o m*/
 * @throws GridException Thrown if an error occurs while marshalling.
 * @throws AmazonClientException If an error occurs while querying Amazon S3.
 */
private void write(GridS3CheckpointData data) throws GridException, AmazonClientException {
    assert data != null;

    if (log.isDebugEnabled())
        log.debug("Writing data to S3 [bucket=" + bucketName + ", key=" + data.getKey() + ']');

    byte[] buf = marsh.marshal(data);

    ObjectMetadata meta = new ObjectMetadata();

    meta.setContentLength(buf.length);

    s3.putObject(bucketName, data.getKey(), new ByteArrayInputStream(buf), meta);
}

From source file:org.gytheio.content.handler.s3.S3ContentReferenceHandlerImpl.java

License:Open Source License

@Override
public long putInputStream(InputStream sourceInputStream, ContentReference targetContentReference)
        throws ContentIOException {
    if (!isContentReferenceSupported(targetContentReference)) {
        throw new ContentIOException("ContentReference not supported");
    }/*from w  w w  . j  a va  2s  . c o m*/

    String remotePath = getRelativePath(targetContentReference.getUri());

    try {
        s3.putObject(new PutObjectRequest(s3BucketName, remotePath, sourceInputStream, new ObjectMetadata()));
        ObjectMetadata metadata = s3.getObjectMetadata(new GetObjectMetadataRequest(s3BucketName, remotePath));
        return metadata.getContentLength();
    } catch (AmazonClientException e) {
        throw new ContentIOException("Failed to write content", e);
    }
}

From source file:org.icgc.dcc.storage.server.repository.s3.S3UploadStateStore.java

License:Open Source License

@Override
public void create(@NonNull ObjectSpecification spec) {
    val uploadStateKey = getUploadStateKey(spec.getObjectId(), spec.getUploadId(), META);

    try {/*w  w  w .j a  v a 2 s. c o  m*/
        byte[] content = MAPPER.writeValueAsBytes(spec);
        val data = new ByteArrayInputStream(content);
        val meta = new ObjectMetadata();
        meta.setContentLength(content.length);

        s3Client.putObject(bucketNamingService.getStateBucketName(spec.getObjectId()), uploadStateKey, data,
                meta);
    } catch (AmazonServiceException e) {
        log.error("Failed to create meta file for spec: {}: {}", spec, e);
        throw new RetryableException(e);
    } catch (IOException e) {
        log.error("Failed to create meta file for spec: {}: {}", spec, e);
        throw new NotRetryableException(e);
    }
}

From source file:org.icgc.dcc.storage.server.repository.s3.S3UploadStateStore.java

License:Open Source License

@Override
public void finalizeUploadPart(String objectId, String uploadId, int partNumber, String md5, String eTag) {
    try {//from   ww w  .jav a  2 s .co  m
        log.debug("Finalizing part for object id: {}, upload id: {}, md5: {}, eTag: {}", objectId, uploadId,
                md5, eTag);
        val json = MAPPER.writeValueAsString(new CompletedPart(partNumber, md5, eTag));
        val partName = formatUploadPartName(partNumber, json);

        val meta = new ObjectMetadata();
        meta.setContentLength(0);
        ByteArrayInputStream data = new ByteArrayInputStream(new byte[0]);
        val uploadStateKey = getUploadStateKey(objectId, uploadId, partName);

        s3Client.putObject(bucketNamingService.getStateBucketName(objectId), uploadStateKey, data, meta);
    } catch (AmazonServiceException e) {
        // TODO: Log args
        log.error("Storage failed", e);
        throw new RetryableException(e);
    } catch (JsonParseException | JsonMappingException e) {
        // TODO: Log
        throw new NotRetryableException(e);
    } catch (IOException e) {
        log.error("Failed to finalize upload part: {}, uploadId: {}, partNumber: {}", objectId, uploadId,
                partNumber, e);
        throw new InternalUnrecoverableError();
    }
}

From source file:org.kuali.maven.wagon.S3Wagon.java

License:Educational Community License

protected ObjectMetadata getObjectMetadata(final File source, final String destination) {
    // Set the mime type according to the extension of the destination file
    String contentType = mimeTypes.getMimetype(destination);
    long contentLength = source.length();

    ObjectMetadata omd = new ObjectMetadata();
    omd.setContentLength(contentLength);
    omd.setContentType(contentType);/*from  w w w . ja v a2 s  . c  o m*/
    return omd;
}

From source file:org.kuali.rice.kew.notes.service.impl.AmazonS3AttachmentServiceImpl.java

License:Educational Community License

@Override
public void persistAttachedFileAndSetAttachmentBusinessObjectValue(Attachment attachment) throws Exception {
    if (attachment.getFileLoc() == null) {
        String s3Url = generateS3Url(attachment);
        attachment.setFileLoc(s3Url);//from w w  w  .  j  a v  a 2 s . co m
    }
    TransferManager manager = new TransferManager(this.amazonS3);
    ObjectMetadata metadata = new ObjectMetadata();
    if (attachment.getMimeType() != null) {
        metadata.setContentType(attachment.getMimeType());
    }
    if (attachment.getFileName() != null) {
        metadata.setContentDisposition(
                "attachment; filename=" + URLEncoder.encode(attachment.getFileName(), "UTF-8"));
    }
    Upload upload = manager.upload(this.bucketName, parseObjectKey(attachment.getFileLoc()),
            attachment.getAttachedObject(), metadata);
    upload.waitForCompletion();
}