List of usage examples for com.amazonaws.services.s3.model ObjectMetadata setContentLength
public void setContentLength(long contentLength)
From source file:org.finra.dm.dao.impl.MockS3OperationsImpl.java
License:Apache License
/** * <p>/*from w w w .ja va2 s.co m*/ * Creates and returns a new {@link ObjectMetadata} with the given parameters. Content length is defaulted to 1 bytes unless a hint is provided. * </p> * <p> * Takes the following hints when filePath is suffixed: * </p> * <dl> * <p/> * <dt>AMAZON_THROTTLING_EXCEPTION</dt> * <dd>Throws AmazonServiceException with the error code "ThrottlingException"</dd> * <p/> * <dt>MOCK_S3_FILE_NAME_SERVICE_EXCEPTION</dt> * <dd>Throws AmazonServiceException</dd> * <p/> * <dt>MOCK_S3_FILE_NAME_NOT_FOUND</dt> * <dd>Throws AmazonServiceException with status code SC_NOT_FOUND</dd> * <p/> * <dt>MOCK_S3_FILE_NAME_0_BYTE_SIZE</dt> * <dd>Sets content length to 0 bytes</dd> * <p/> * </dl> */ @Override public ObjectMetadata getObjectMetadata(String sourceBucketName, String filePath, AmazonS3Client s3Client) { ObjectMetadata objectMetadata = new ObjectMetadata(); if (filePath.endsWith(MockAwsOperationsHelper.AMAZON_THROTTLING_EXCEPTION)) { AmazonServiceException throttlingException = new AmazonServiceException("test throttling exception"); throttlingException.setErrorCode("ThrottlingException"); throw throttlingException; } else if (filePath.endsWith(MOCK_S3_FILE_NAME_SERVICE_EXCEPTION)) { throw new AmazonServiceException(null); } else if (filePath.endsWith(MOCK_S3_FILE_NAME_NOT_FOUND)) { AmazonServiceException exception = new AmazonServiceException(null); exception.setStatusCode(HttpStatus.SC_NOT_FOUND); throw exception; } else if (filePath.endsWith(MOCK_S3_FILE_NAME_0_BYTE_SIZE)) { objectMetadata.setContentLength(AbstractCoreTest.FILE_SIZE_0_BYTE); } else { objectMetadata.setContentLength(AbstractCoreTest.FILE_SIZE_1_KB); } return objectMetadata; }
From source file:org.finra.dm.dao.impl.MockS3OperationsImpl.java
License:Apache License
/** * Puts an object into a bucket. Creates a new bucket if the bucket does not already exist. * * @throws IllegalArgumentException when there is an error reading from input stream. *//* w w w .jav a 2 s .co m*/ @Override public PutObjectResult putObject(PutObjectRequest putObjectRequest, AmazonS3Client s3Client) { LOGGER.debug("putObject(): putObjectRequest.getBucketName() = " + putObjectRequest.getBucketName() + ", putObjectRequest.getKey() = " + putObjectRequest.getKey()); String s3BucketName = putObjectRequest.getBucketName(); InputStream inputStream = putObjectRequest.getInputStream(); ObjectMetadata metadata = putObjectRequest.getMetadata(); if (metadata == null) { metadata = new ObjectMetadata(); } File file = putObjectRequest.getFile(); if (file != null) { try { inputStream = new FileInputStream(file); metadata.setContentLength(file.length()); } catch (FileNotFoundException e) { throw new IllegalArgumentException("File not found " + file, e); } } String s3ObjectKey = putObjectRequest.getKey(); byte[] s3ObjectData; try { s3ObjectData = IOUtils.toByteArray(inputStream); } catch (IOException e) { throw new IllegalArgumentException("Error converting input stream into byte array", e); } finally { try { inputStream.close(); } catch (IOException e) { LOGGER.error("Error closing stream " + inputStream, e); } } MockS3Bucket mockS3Bucket = getOrCreateBucket(s3BucketName); MockS3Object mockS3Object = new MockS3Object(); mockS3Object.setKey(s3ObjectKey); mockS3Object.setData(s3ObjectData); mockS3Object.setObjectMetadata(metadata); mockS3Bucket.getObjects().put(s3ObjectKey, mockS3Object); return new PutObjectResult(); }
From source file:org.finra.dm.dao.impl.S3DaoImpl.java
License:Apache License
@Override public void createDirectory(final S3FileTransferRequestParamsDto params) { // Create metadata for the directory marker and set content-length to 0 bytes. ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(0); prepareMetadata(params, metadata);/* ww w . j a va 2s.c o m*/ // Create empty content. InputStream emptyContent = new ByteArrayInputStream(new byte[0]); // Create a PutObjectRequest passing the folder name suffixed by '/'. String directoryName = params.getS3KeyPrefix() + (params.getS3KeyPrefix().endsWith("/") ? "" : "/"); PutObjectRequest putObjectRequest = new PutObjectRequest(params.getS3BucketName(), directoryName, emptyContent, metadata); AmazonS3Client s3Client = null; try { s3Client = getAmazonS3(params); s3Operations.putObject(putObjectRequest, s3Client); } catch (AmazonServiceException e) { throw new IllegalStateException( String.format("Failed to create 0 byte S3 object with \"%s\" key in bucket \"%s\". Reason: %s", directoryName, params.getS3BucketName(), e.getMessage()), e); } finally { // Shutdown the AmazonS3Client instance to release resources. if (s3Client != null) { s3Client.shutdown(); } } }
From source file:org.finra.herd.dao.impl.MockS3OperationsImpl.java
License:Apache License
/** * {@inheritDoc}/*from ww w . ja va2 s. c o m*/ * <p/> * This implementation creates a new bucket if the bucket does not already exist. */ @Override public PutObjectResult putObject(PutObjectRequest putObjectRequest, AmazonS3 s3Client) { LOGGER.debug("putObject(): putObjectRequest.getBucketName() = " + putObjectRequest.getBucketName() + ", putObjectRequest.getKey() = " + putObjectRequest.getKey()); String s3BucketName = putObjectRequest.getBucketName(); InputStream inputStream = putObjectRequest.getInputStream(); ObjectMetadata metadata = putObjectRequest.getMetadata(); if (metadata == null) { metadata = new ObjectMetadata(); } File file = putObjectRequest.getFile(); if (file != null) { try { inputStream = new FileInputStream(file); metadata.setContentLength(file.length()); } catch (FileNotFoundException e) { throw new IllegalArgumentException("File not found " + file, e); } } String s3ObjectKey = putObjectRequest.getKey(); String s3ObjectVersion = MOCK_S3_BUCKET_NAME_VERSIONING_ENABLED.equals(putObjectRequest.getBucketName()) ? UUID.randomUUID().toString() : null; String s3ObjectKeyVersion = s3ObjectKey + (s3ObjectVersion != null ? s3ObjectVersion : ""); byte[] s3ObjectData; try { s3ObjectData = IOUtils.toByteArray(inputStream); metadata.setContentLength(s3ObjectData.length); } catch (IOException e) { throw new IllegalArgumentException("Error converting input stream into byte array", e); } finally { try { inputStream.close(); } catch (IOException e) { LOGGER.error("Error closing stream " + inputStream, e); } } // Update the Last-Modified header value. This value not being set causes NullPointerException in S3Dao download related unit tests. metadata.setLastModified(new Date()); MockS3Bucket mockS3Bucket = getOrCreateBucket(s3BucketName); MockS3Object mockS3Object = new MockS3Object(); mockS3Object.setKey(s3ObjectKey); mockS3Object.setVersion(s3ObjectVersion); mockS3Object.setData(s3ObjectData); mockS3Object.setObjectMetadata(metadata); if (putObjectRequest.getTagging() != null) { mockS3Object.setTags(putObjectRequest.getTagging().getTagSet()); } mockS3Bucket.getObjects().put(s3ObjectKey, mockS3Object); mockS3Bucket.getVersions().put(s3ObjectKeyVersion, mockS3Object); return new PutObjectResult(); }
From source file:org.finra.herd.dao.impl.S3DaoImpl.java
License:Apache License
@Override public void createDirectory(final S3FileTransferRequestParamsDto params) { // Create metadata for the directory marker and set content-length to 0 bytes. ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(0); prepareMetadata(params, metadata);/* w w w. j av a 2 s. co m*/ // Create empty content. InputStream emptyContent = new ByteArrayInputStream(new byte[0]); // Create a PutObjectRequest passing the folder name suffixed by '/'. String directoryName = StringUtils.appendIfMissing(params.getS3KeyPrefix(), "/"); PutObjectRequest putObjectRequest = new PutObjectRequest(params.getS3BucketName(), directoryName, emptyContent, metadata); // KMS key ID is being set through prepareMetadata() AmazonS3Client s3Client = getAmazonS3(params); try { s3Operations.putObject(putObjectRequest, s3Client); } catch (AmazonServiceException e) { throw new IllegalStateException( String.format("Failed to create 0 byte S3 object with \"%s\" key in bucket \"%s\". Reason: %s", directoryName, params.getS3BucketName(), e.getMessage()), e); } finally { // Shutdown the AmazonS3Client instance to release resources. s3Client.shutdown(); } }
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);// ww w. j a v a2s.com 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 ww . j a v a 2 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 {/* ww w . j a v a 2 s .c o m*/ 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./* w w w . j av a2 s . co 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.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 . j ava2 s.c om*/ return omd; }