BufferedImage image = ... ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(image, "png", os); byte[] buffer = os.toByteArray(); InputStream is = new ByteArrayInputStream(buffer); AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(cred)); ObjectMetadata meta = new ObjectMetadata(); meta.setContentLength(buffer.length); s3.putObject(new PutObjectRequest("folder", key, is, meta));
@Override protected boolean createEmptyObject(String key) { try { ObjectMetadata meta = new ObjectMetadata(); meta.setContentLength(0); meta.setContentMD5(DIR_HASH); meta.setContentType(Mimetypes.MIMETYPE_OCTET_STREAM); mClient.putObject( new PutObjectRequest(mBucketName, key, new ByteArrayInputStream(new byte[0]), meta)); return true; } catch (AmazonClientException e) { LOG.error("Failed to create object: {}", key, e); return false; } }
try { S3Object object = getS3ObjectAndMetadata(bucket, key, ssecLocal); int sizeOfFile = (int)object.getObjectMetadata().getContentLength(); fieldCount = sizeOfFile/sizeArray; totalSize = sizeOfFile; offset += sizeArray; try (InputStream input = new ByteArrayInputStream(destinationArray)) { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(totalSize); PutObjectRequest putObjectRequest = null; if (sseLocal.equals("true")) { metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); putObjectRequest = new PutObjectRequest(bucket, key, input, metadata); } else if (ssecLocal != null) { putObjectRequest = new PutObjectRequest(bucket, key, input, metadata).withSSECustomerKey(ssecLocal); } else { putObjectRequest = new PutObjectRequest(bucket, key, s3Client.putObject(putObjectRequest); if(res.getETag() == null) { return Status.ERROR;
@Override public PutObjectRequest decorate(PutObjectRequest request) { final ObjectMetadata objectMetadata = request.getMetadata() == null ? new ObjectMetadata() : request.getMetadata().clone(); objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); return request.withMetadata(objectMetadata); }
private static long getObjectSize(Path path, ObjectMetadata metadata) throws IOException { Map<String, String> userMetadata = metadata.getUserMetadata(); String length = userMetadata.get(UNENCRYPTED_CONTENT_LENGTH); if (userMetadata.containsKey(SERVER_SIDE_ENCRYPTION) && length == null) { throw new IOException(format("%s header is not set on an encrypted object: %s", UNENCRYPTED_CONTENT_LENGTH, path)); } return (length != null) ? Long.parseLong(length) : metadata.getContentLength(); }
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); PutObjectResult putObjectResult = client.putObject(putObjectRequest); if ( !putObjectResult.getETag().equals(S3Utils.toHex(md5)) ) { throw new Exception("Unable to match MD5 for config"); } return metadata; } }
@Test public void testSignPutObject() throws URISyntaxException { final AWSS3V4Signer signer = new S3SignerWithDateOverride(new Date(1431115356859L)); // THESE ARE BOGUS CREDENTIALS final AWSCredentials credentials = new BasicAWSCredentials( "AKIAJd4scjDDmxXZTESTGOZQ", "LYd/ad4scjDDmxXZTESTtRz7xdOM1SiD6"); final ByteArrayInputStream bais = new ByteArrayInputStream("content".getBytes(StringUtils.UTF8)); final ObjectMetadata om = new ObjectMetadata(); om.setContentLength("content".getBytes(StringUtils.UTF8).length); final PutObjectRequest por = new PutObjectRequest("test-bucket123456", "key", bais, om); final Request<?> pr = new DefaultRequest(por, Constants.S3_SERVICE_DISPLAY_NAME); pr.setContent(bais); pr.setResourcePath("key"); pr.setHttpMethod(HttpMethodName.PUT); pr.addHeader(Headers.CONTENT_LENGTH, String.valueOf(bais.available())); pr.setEndpoint(new URI("https://test-bucket123456.s3-us-west-2.amazonaws.com")); pr.addHeader("Host", "test-bucket123456.s3-us-west-2.amazonaws.com"); signer.sign(pr, credentials); assertEquals(getSignature(pr), "e0a8ac165c54dc1fc3dd987f5e00b44f1b91f3c63b05ee642432e1f3c7286d69"); }
@Override public LoadedInstanceConfig loadConfig() throws Exception { Date lastModified; Properties properties = new Properties(); S3Object object = getConfigObject(); if ( object != null ) { try { lastModified = object.getObjectMetadata().getLastModified(); properties.load(object.getObjectContent()); } finally { CloseableUtils.closeQuietly(object.getObjectContent()); } } else { lastModified = new Date(0L); } PropertyBasedInstanceConfig config = new PropertyBasedInstanceConfig(properties, defaults); return new LoadedInstanceConfig(config, lastModified.getTime()); }
ObjectMetadata from = new ObjectMetadata(); from.setCacheControl("cachecontrol"); from.setContentDisposition("contentDisposition"); from.setContentEncoding("contentEncoding"); from.setContentLength(1234); from.setContentMD5("md5"); from.setContentType("contentType"); from.setExpirationTime(expTime); from.setExpirationTimeRuleId("expirationTimeRuleId"); from.setHeader("k1", "v1"); from.setHeader("k2", "v2"); from.setHttpExpiresDate(httpExpiresDate); from.setLastModified(lastModified); from.setOngoingRestore(true); from.setRestoreExpirationTime(restoreExpirationTime); from.setSSEAlgorithm("ssealgo"); from.setSSECustomerKeyMd5("sseCustKeyMd5"); from.setSSECustomerAlgorithm("SSECustomerAlgorithm"); from.setUserMetadata(userMetadata); from.setHeader(Headers.CONTENT_RANGE, "/9999"); from.setHeader(Headers.S3_VERSION_ID, "versionid"); from.setHeader(Headers.ETAG, "etag"); from.setHeader(Headers.STORAGE_CLASS, StorageClass.ReducedRedundancy.toString()); assertEquals("cachecontrol", from.getCacheControl()); assertEquals("contentDisposition", from.getContentDisposition()); assertEquals("contentEncoding", from.getContentEncoding()); assertTrue(1234 == from.getContentLength()); assertEquals("md5", from.getContentMD5());
final Long lastFullyDownloadedPartPosition) assertParameterNotNull(getObjectRequest, "A valid GetObjectRequest must be provided to initiate download"); assertParameterNotNull(file, "A valid file must be provided to download into"); appendSingleObjectUserAgent(getObjectRequest); String description = "Downloading from " + getObjectRequest.getBucketName() + "/" + getObjectRequest.getKey(); final ObjectMetadata objectMetadata = s3.getObjectMetadata(getObjectMetadataRequest); long lastModifiedTime = objectMetadata.getLastModified().getTime(); lastByte = range[1]; } else { lastByte = objectMetadata.getContentLength() - 1; getObjectRequest.setUnmodifiedSinceConstraint(new Date(lastModifiedTime));
indexObject ); final ByteArrayInputStream bais = new ByteArrayInputStream( StringUtils.toUtf8(jsonMapper.writeValueAsString(dataSegment)) ); timestampedSegments, dataSegment, objectMetadata.getLastModified() == null ? 0 : objectMetadata.getLastModified().getTime() );
private static long lastModifiedTime(ObjectMetadata metadata) { Date date = metadata.getLastModified(); return (date != null) ? date.getTime() : 0; }
/** * Is the S3 object newer than the timestamp passed in? */ @Override protected boolean isRemoteResourceNewer(final String resourceName, final long timestamp) { ObjectMetadata metadata = client.getObjectMetadata(bucketName, basedir + resourceName); return metadata.getLastModified().compareTo(new Date(timestamp)) < 0; }
final ObjectMetadata om = new ObjectMetadata(); om.setContentLength(file.length()); om.setCacheControl(upload.headerCacheControl); om.setContentDisposition(upload.headerContentDisposition); om.setContentEncoding(upload.headerContentEncoding); om.setContentType(upload.headerContentType); } else { om.setContentType(Mimetypes.getInstance().getMimetype(file)); putObjectRequest.setStorageClass(upload.headerStorageClass); om.setExpirationTimeRuleId(upload.expirationTimeRuleId); om.setHttpExpiresDate(new Date(Long.valueOf(upload.httpExpires))); om.setSSEAlgorithm(upload.sseAlgorithm); om.setUserMetadata(upload.userMetadata); tagList.add(new Tag(tagParts[0], tagParts[1])); om.setContentMD5(upload.md5);
public PutObjectResult putObject(String bucket, String key, String content) { final InputStream in = new ByteArrayInputStream(StringUtils.toUtf8(content)); return putObject(new PutObjectRequest(bucket, key, in, new ObjectMetadata())); }
/** * Updates put request to store the specified instruction object in S3. * * @param req * The put-instruction-file request for the instruction file to * be stored in S3. * @param cekMaterial * The instruction object to be stored in S3. * @return A put request to store the specified instruction object in S3. */ protected final PutObjectRequest updateInstructionPutRequest( PutObjectRequest req, ContentCryptoMaterial cekMaterial) { byte[] bytes = cekMaterial.toJsonString(cryptoConfig.getCryptoMode()) .getBytes(UTF8); ObjectMetadata metadata = req.getMetadata(); if (metadata == null) { metadata = new ObjectMetadata(); req.setMetadata(metadata); } // Set the content-length of the upload metadata.setContentLength(bytes.length); // Set the crypto instruction file header metadata.addUserMetadata(Headers.CRYPTO_INSTRUCTION_FILE, ""); // Update the instruction request req.setMetadata(metadata); req.setInputStream(new ByteArrayInputStream(bytes)); // the file attribute in the request is always null before calling this // routine return req; }
InputStream input = isOrig; if (metadata == null) metadata = new ObjectMetadata(); rejectNull(bucketName, "The bucket name parameter must be specified when uploading an object"); metadata.setContentLength(file.length()); final boolean calculateMD5 = metadata.getContentMD5() == null; if (metadata.getContentType() == null) { metadata.setContentType(Mimetypes.getInstance().getMimetype(file)); try { String contentMd5_b64 = Md5Utils.md5AsBase64(file); metadata.setContentMD5(contentMd5_b64); } catch (Exception e) { throw new SdkClientException( final Long contentLength = (Long)metadata.getRawMetadataValue(Headers.CONTENT_LENGTH); if (contentLength == null) { if (metadata.getContentMD5() == null && !skipMd5CheckStrategy.skipClientSideValidationPerRequest(putObjectRequest)) { if (metadata.getContentType() == null) { metadata.setContentType(Mimetypes.MIMETYPE_OCTET_STREAM); String contentMd5 = metadata.getContentMD5(); if (md5DigestStream != null) {
rateLimiter.acquire(chunk.length); ObjectMetadata objectMetadata = getObjectMetadata(localPath); objectMetadata.setContentLength(chunk.length); PutObjectRequest putObjectRequest = new PutObjectRequest( config.getBackupPrefix(), remotePath.toString(), new ByteArrayInputStream(chunk), objectMetadata);
InputStream awsInputStream = new ByteArrayInputStream(data); ObjectMetadata om = new ObjectMetadata(); om.setContentLength(written); om.setContentType( mimeType ); PutObjectResult result = null; result = getS3Client().putObject( bucketName, uploadFileName, awsInputStream, om ); InitiateMultipartUploadResult initResponse = getS3Client().initiateMultipartUpload( initRequest ); InputStream firstChunck = new ByteArrayInputStream(data); PushbackInputStream chunckableInputStream = new PushbackInputStream(inputStream, 1); .withPartSize(partSize) .withLastPart(isLastPart); partETags.add( getS3Client().uploadPart(uploadRequest).getPartETag() ); partNumber++;
metadata = new ObjectMetadata(); metadata.setContentLength(file.length()); final boolean calculateMD5 = metadata.getContentMD5() == null; if (metadata.getContentType() == null) { metadata.setContentType(Mimetypes.getInstance().getMimetype(file)); try { final String contentMd5_b64 = Md5Utils.md5AsBase64(file); metadata.setContentMD5(contentMd5_b64); } catch (final Exception e) { throw new AmazonClientException( final Long contentLength = (Long) metadata.getRawMetadataValue(Headers.CONTENT_LENGTH); if (contentLength == null) { if (metadata.getContentMD5() == null && !skipContentMd5Check) { if (metadata.getContentType() == null) { metadata.setContentType(Mimetypes.MIMETYPE_OCTET_STREAM); String contentMd5 = metadata.getContentMD5(); if (md5DigestStream != null) { contentMd5 = BinaryUtils.toBase64(md5DigestStream.getMd5Digest());