public void upload(final String bucketName, final File file) { final PutObjectResponse putObjectResponse = s3Client.putObject(PutObjectRequest.builder() .bucket(bucketName) .key(file.getName()) .build(), file.toPath()); LOG.debug("upload {} to bucket {}: ", file.getName(), bucketName, putObjectResponse.toString()); }
/** * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be * redacted from this string using a placeholder value. */ @Override public String toString() { return ToString.builder("PutObjectRequest").add("ACL", aclAsString()).add("Bucket", bucket()) .add("CacheControl", cacheControl()).add("ContentDisposition", contentDisposition()) .add("ContentEncoding", contentEncoding()).add("ContentLanguage", contentLanguage()) .add("ContentLength", contentLength()).add("ContentMD5", contentMD5()).add("ContentType", contentType()) .add("Expires", expires()).add("GrantFullControl", grantFullControl()).add("GrantRead", grantRead()) .add("GrantReadACP", grantReadACP()).add("GrantWriteACP", grantWriteACP()).add("Key", key()) .add("Metadata", metadata()).add("ServerSideEncryption", serverSideEncryptionAsString()) .add("StorageClass", storageClassAsString()).add("WebsiteRedirectLocation", websiteRedirectLocation()) .add("SSECustomerAlgorithm", sseCustomerAlgorithm()) .add("SSECustomerKey", sseCustomerKey() == null ? null : "*** Sensitive Data Redacted ***") .add("SSECustomerKeyMD5", sseCustomerKeyMD5()) .add("SSEKMSKeyId", ssekmsKeyId() == null ? null : "*** Sensitive Data Redacted ***") .add("RequestPayer", requestPayerAsString()).add("Tagging", tagging()) .add("ObjectLockMode", objectLockModeAsString()).add("ObjectLockRetainUntilDate", objectLockRetainUntilDate()) .add("ObjectLockLegalHoldStatus", objectLockLegalHoldStatusAsString()).build(); }
@Override public PutObjectRequest build() { return new PutObjectRequest(this); }
@Override public PutObjectResponse putObject(final PutObjectRequest putObjectRequest, final RequestBody requestBody) throws S3Exception { try { bucketsWithContents.get(putObjectRequest.bucket()).put(putObjectRequest.key(), bucketItemBuilder() .withName(putObjectRequest.key()) .withData(toByteArray(requestBody.contentStreamProvider().newStream())) .withLastModifiedNow() .build()); return PutObjectResponse.builder().build(); } catch (IOException e) { throw new RuntimeException(e); } }
return putObject(PutObjectRequest.builder().applyMutation(putObjectRequest).build(), filePath);
return Objects.equals(aclAsString(), other.aclAsString()) && Objects.equals(bucket(), other.bucket()) && Objects.equals(cacheControl(), other.cacheControl()) && Objects.equals(contentDisposition(), other.contentDisposition()) && Objects.equals(contentEncoding(), other.contentEncoding()) && Objects.equals(contentLanguage(), other.contentLanguage()) && Objects.equals(contentLength(), other.contentLength()) && Objects.equals(contentMD5(), other.contentMD5()) && Objects.equals(contentType(), other.contentType()) && Objects.equals(expires(), other.expires()) && Objects.equals(grantFullControl(), other.grantFullControl()) && Objects.equals(grantRead(), other.grantRead()) && Objects.equals(grantReadACP(), other.grantReadACP()) && Objects.equals(grantWriteACP(), other.grantWriteACP()) && Objects.equals(key(), other.key()) && Objects.equals(metadata(), other.metadata()) && Objects.equals(serverSideEncryptionAsString(), other.serverSideEncryptionAsString()) && Objects.equals(storageClassAsString(), other.storageClassAsString()) && Objects.equals(websiteRedirectLocation(), other.websiteRedirectLocation()) && Objects.equals(sseCustomerAlgorithm(), other.sseCustomerAlgorithm()) && Objects.equals(sseCustomerKey(), other.sseCustomerKey()) && Objects.equals(sseCustomerKeyMD5(), other.sseCustomerKeyMD5()) && Objects.equals(ssekmsKeyId(), other.ssekmsKeyId()) && Objects.equals(requestPayerAsString(), other.requestPayerAsString()) && Objects.equals(tagging(), other.tagging()) && Objects.equals(objectLockModeAsString(), other.objectLockModeAsString()) && Objects.equals(objectLockRetainUntilDate(), other.objectLockRetainUntilDate()) && Objects.equals(objectLockLegalHoldStatusAsString(), other.objectLockLegalHoldStatusAsString());
return putObject(PutObjectRequest.builder().applyMutation(putObjectRequest).build(), sourcePath);
@Override public int hashCode() { int hashCode = 1; hashCode = 31 * hashCode + Objects.hashCode(aclAsString()); hashCode = 31 * hashCode + Objects.hashCode(bucket()); hashCode = 31 * hashCode + Objects.hashCode(cacheControl()); hashCode = 31 * hashCode + Objects.hashCode(contentDisposition()); hashCode = 31 * hashCode + Objects.hashCode(contentEncoding()); hashCode = 31 * hashCode + Objects.hashCode(contentLanguage()); hashCode = 31 * hashCode + Objects.hashCode(contentLength()); hashCode = 31 * hashCode + Objects.hashCode(contentMD5()); hashCode = 31 * hashCode + Objects.hashCode(contentType()); hashCode = 31 * hashCode + Objects.hashCode(expires()); hashCode = 31 * hashCode + Objects.hashCode(grantFullControl()); hashCode = 31 * hashCode + Objects.hashCode(grantRead()); hashCode = 31 * hashCode + Objects.hashCode(grantReadACP()); hashCode = 31 * hashCode + Objects.hashCode(grantWriteACP()); hashCode = 31 * hashCode + Objects.hashCode(key()); hashCode = 31 * hashCode + Objects.hashCode(metadata()); hashCode = 31 * hashCode + Objects.hashCode(serverSideEncryptionAsString()); hashCode = 31 * hashCode + Objects.hashCode(storageClassAsString()); hashCode = 31 * hashCode + Objects.hashCode(websiteRedirectLocation()); hashCode = 31 * hashCode + Objects.hashCode(sseCustomerAlgorithm()); hashCode = 31 * hashCode + Objects.hashCode(sseCustomerKey()); hashCode = 31 * hashCode + Objects.hashCode(sseCustomerKeyMD5()); hashCode = 31 * hashCode + Objects.hashCode(ssekmsKeyId()); hashCode = 31 * hashCode + Objects.hashCode(requestPayerAsString()); hashCode = 31 * hashCode + Objects.hashCode(tagging()); hashCode = 31 * hashCode + Objects.hashCode(objectLockModeAsString()); hashCode = 31 * hashCode + Objects.hashCode(objectLockRetainUntilDate());
return putObject(PutObjectRequest.builder().applyMutation(putObjectRequest).build(), requestBody);
public <T> Optional<T> getValueForField(String fieldName, Class<T> clazz) { switch (fieldName) { case "ACL": return Optional.ofNullable(clazz.cast(aclAsString())); case "Bucket": return Optional.ofNullable(clazz.cast(bucket())); case "CacheControl": return Optional.ofNullable(clazz.cast(cacheControl())); case "ContentDisposition": return Optional.ofNullable(clazz.cast(contentDisposition())); case "ContentEncoding": return Optional.ofNullable(clazz.cast(contentEncoding())); case "ContentLanguage": return Optional.ofNullable(clazz.cast(contentLanguage())); case "ContentLength": return Optional.ofNullable(clazz.cast(contentLength())); case "ContentMD5": return Optional.ofNullable(clazz.cast(contentMD5())); case "ContentType": return Optional.ofNullable(clazz.cast(contentType())); case "Expires": return Optional.ofNullable(clazz.cast(expires())); case "GrantFullControl": return Optional.ofNullable(clazz.cast(grantFullControl())); case "GrantRead": return Optional.ofNullable(clazz.cast(grantRead())); case "GrantReadACP": return Optional.ofNullable(clazz.cast(grantReadACP())); case "GrantWriteACP": return Optional.ofNullable(clazz.cast(grantWriteACP()));
return putObject(PutObjectRequest.builder().applyMutation(putObjectRequest).build(), requestBody);
@Override public void setFeatureState(final FeatureState featureState) { try { final FeatureStateStorageWrapper storageWrapper = FeatureStateStorageWrapper.wrapperForFeatureState(featureState); final String json = objectMapper.writeValueAsString(storageWrapper); final PutObjectRequest putObjectRequest = PutObjectRequest.builder() .bucket(s3TogglzProperties.getBucketName()) .key(keyForFeature(featureState.getFeature())) .serverSideEncryption(ServerSideEncryption.AES256) .build(); final RequestBody requestBody = RequestBody.fromString(json); s3Client.putObject(putObjectRequest, requestBody); } catch (S3Exception | JsonProcessingException e) { throw new RuntimeException("Failed to set the feature state", e); } }
public void persistFeatureStateToS3(final FeatureState featureState) { try { final FeatureStateStorageWrapper storageWrapper = FeatureStateStorageWrapper.wrapperForFeatureState(featureState); final String json = objectMapper.writeValueAsString(storageWrapper); final PutObjectRequest putObjectRequest = PutObjectRequest.builder() .bucket(togglzProperties.getS3().getBucketName()) .key(keyForFeature(featureState.getFeature())) .serverSideEncryption(ServerSideEncryption.AES256) .build(); final RequestBody requestBody = RequestBody.fromString(json); s3Client.putObject(putObjectRequest, requestBody); } catch (final S3Exception | JsonProcessingException e) { throw new RuntimeException("Failed to set the feature state", e); } }
@Override public Completable store(Flowable<Buffer> stream, String hashsum) { return Completable.create(sub -> { PutObjectRequest request = PutObjectRequest.builder() .bucket(options.getBucketName()) .key(hashsum)
@Test public void getObjectShouldCreateFileWithData() throws Exception { // given testee.putObject(PutObjectRequest.builder() .bucket("someBucket") .key("someKey") .build(), RequestBody.fromString("testdata")); //when Path tempFile = Files.createTempFile("test", "tmp"); testee.getObject(GetObjectRequest.builder() .bucket("someBucket") .key("someKey") .build(), tempFile); //then List<String> lines = Files.readAllLines(tempFile); assertThat(lines.get(0), is("testdata")); }
@Test public void shouldListObjectsInBucket() { // given PutObjectRequest putObjectRequest = PutObjectRequest.builder() .bucket("someBucket") .key("someObject") .build(); RequestBody requestBody = RequestBody.fromString("content"); testee.putObject(putObjectRequest, requestBody); // when ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder() .bucket("someBucket") .build(); ListObjectsV2Response listObjectsV2Response = testee.listObjectsV2(listObjectsV2Request); //then assertThat(listObjectsV2Response.contents().size(), is(1)); assertThat(listObjectsV2Response.contents().get(0).key(), is("someObject")); }
@Test public void getObjectShouldReturnStreamWithData() throws Exception { // given testee.putObject(PutObjectRequest.builder() .bucket("someBucket") .key("someKey") .build(), RequestBody.fromString("testdata")); //when ResponseInputStream<GetObjectResponse> inputStream = testee.getObject(GetObjectRequest.builder() .bucket("someBucket") .key("someKey") .build()); //then String data = IoUtils.toUtf8String(inputStream); assertThat(data, is("testdata")); }
@Test public void deleteShouldRemoveItemsFromBucket() { // given PutObjectRequest putObjectRequest = PutObjectRequest.builder() .bucket("someBucket") .key("someObject") .build(); RequestBody requestBody = RequestBody.fromString("content"); testee.putObject(putObjectRequest, requestBody); testee.deleteObjects(DeleteObjectsRequest.builder().bucket("someBucket").delete(Delete.builder().objects (ObjectIdentifier.builder().key("someObject").build()).build()).build()); // when ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder() .bucket("someBucket") .build(); ListObjectsV2Response listObjectsV2Response = testee.listObjectsV2(listObjectsV2Request); //then assertThat(listObjectsV2Response.contents().size(), is(0)); }