/** * Upload a file which is in the assets bucket. * * @param fileName File name * @param file File * @param contentType Content type for file * @return */ public static boolean uploadFile(String fileName, File file, String contentType) { try { if (S3Module.amazonS3 != null) { String bucket = S3Module.s3Bucket; ObjectMetadata metaData = new ObjectMetadata(); if (contentType != null) { metaData.setContentType(contentType); } PutObjectRequest putObj = new PutObjectRequest(bucket, fileName, file); putObj.setMetadata(metaData); putObj.withCannedAcl(CannedAccessControlList.PublicRead); S3Module.amazonS3.putObject(putObj); return true; } else { Logger.error("Could not save because amazonS3 was null"); return false; } } catch (Exception e) { Logger.error("S3 Upload -" + e.getMessage()); return false; } }
public static void uploadToS3() { // upload to s3 bucket AWSCredentials awsCredentials = SkillConfig.getAWSCredentials(); AmazonS3Client s3Client = awsCredentials != null ? new AmazonS3Client(awsCredentials) : new AmazonS3Client(); File folder = new File("c:/temp/morse/" + DOT + "/mp3/"); File[] listOfFiles = folder.listFiles(); for (File file : listOfFiles) { if (file.isFile()) { if (!s3Client.doesObjectExist("morseskill", DOT + "/" + file.getName())) { PutObjectRequest s3Put = new PutObjectRequest("morseskill", DOT + "/" + file.getName(), file).withCannedAcl(CannedAccessControlList.PublicRead); s3Client.putObject(s3Put); System.out.println("Upload complete: " + file.getName()); } else { System.out.println("Skip as " + file.getName() + " already exists."); } } } }
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; }
/** * Stores the {@link InputStream} as an object in the S3 bucket. * * @param keyName The requested key name for the object. * @param inStream The {@link InputStream} to write out to an object in S3. * @param size The size of the {@link InputStream}. * @return A {@link CompletableFuture} that will eventually contain the S3 object key. */ @Override public CompletableFuture<String> store(String keyName, InputStream inStream, long size) { final String bucketName = environment.getProperty(Constants.BUCKET_NAME_ENV_VARIABLE); final String kmsKey = environment.getProperty(Constants.KMS_KEY_ENV_VARIABLE); if (Strings.isNullOrEmpty(bucketName) || Strings.isNullOrEmpty(kmsKey)) { API_LOG.warn("No bucket name is specified or no KMS key specified."); return CompletableFuture.completedFuture(""); } ObjectMetadata s3ObjectMetadata = new ObjectMetadata(); s3ObjectMetadata.setContentLength(size); PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inStream, s3ObjectMetadata) .withSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(kmsKey)); API_LOG.info("Writing object {} to S3 bucket {}", keyName, bucketName); return actOnItem(putObjectRequest); }
/** * Uses the {@link TransferManager} to upload a file. * * @param objectToActOn The put request. * @return The object key in the bucket. */ @Override protected String asynchronousAction(PutObjectRequest objectToActOn) { String returnValue; try { Upload upload = s3TransferManager.upload(objectToActOn); returnValue = upload.waitForUploadResult().getKey(); } catch (InterruptedException exception) { Thread.currentThread().interrupt(); throw new UncheckedInterruptedException(exception); } API_LOG.info("Successfully wrote object {} to S3 bucket {}", returnValue, objectToActOn.getBucketName()); return returnValue; }
@Override public PutObjectResult putObject(PutObjectRequest putObjectRequest) throws AmazonClientException, AmazonServiceException { String blobName = putObjectRequest.getKey(); DigestInputStream stream = (DigestInputStream) putObjectRequest.getInputStream(); if (blobs.containsKey(blobName)) { throw new AmazonS3Exception("[" + blobName + "] already exists."); } blobs.put(blobName, stream); // input and output md5 hashes need to match to avoid an exception String md5 = Base64.encodeAsString(stream.getMessageDigest().digest()); PutObjectResult result = new PutObjectResult(); result.setContentMd5(md5); return result; }
@SuppressWarnings("resource") @Override public PutObjectResult putObject(PutObjectRequest putObjectRequest) throws AmazonClientException, AmazonServiceException { putObjectRequests.add(putObjectRequest); S3Object s3Object = new S3Object(); s3Object.setBucketName(putObjectRequest.getBucketName()); s3Object.setKey(putObjectRequest.getKey()); if (putObjectRequest.getFile() != null) { try { s3Object.setObjectContent(new FileInputStream(putObjectRequest.getFile())); } catch (FileNotFoundException e) { throw new AmazonServiceException("Cannot store the file object.", e); } } else { s3Object.setObjectContent(putObjectRequest.getInputStream()); } objects.add(s3Object); PutObjectResult putObjectResult = new PutObjectResult(); putObjectResult.setETag("3a5c8b1ad448bca04584ecb55b836264"); return putObjectResult; }
@Override public void store(BuildCacheKey key, BuildCacheEntryWriter writer) { logger.info("Start storing cache entry '{}' in S3 bucket", key.getHashCode()); ObjectMetadata meta = new ObjectMetadata(); meta.setContentType(BUILD_CACHE_CONTENT_TYPE); try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { writer.writeTo(os); meta.setContentLength(os.size()); try (InputStream is = new ByteArrayInputStream(os.toByteArray())) { PutObjectRequest request = getPutObjectRequest(key, meta, is); if(this.reducedRedundancy) { request.withStorageClass(StorageClass.ReducedRedundancy); } s3.putObject(request); } } catch (IOException e) { throw new BuildCacheException("Error while storing cache object in S3 bucket", e); } }
/** * The method creates a short URL code for the given url * * @param url * @return */ public ShortUrl createShortUrl(String url, String code) { logger.info("storing the url {} in the bucket {}", url, this.bucket); // Create the link for the short code if (code == null) { code = getObjectCode(); } String loadFile = redirectFile.replace("REPLACE", url); byte[] fileContentBytes = loadFile.getBytes(StandardCharsets.UTF_8); InputStream fileInputStream = new ByteArrayInputStream(fileContentBytes); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentType("text/html"); metadata.addUserMetadata("url", url); metadata.setContentLength(fileContentBytes.length); PutObjectRequest putObjectRequest = new PutObjectRequest(this.bucket, code, fileInputStream, metadata) .withCannedAcl(CannedAccessControlList.PublicRead); this.s3Client.putObject(putObjectRequest); createDummyRecord(url, code); return new ShortUrl(url, code); }
private void createEmptyObject(final String bucketName, final String objectName) throws AmazonClientException, AmazonServiceException { final InputStream im = new InputStream() { @Override public int read() throws IOException { return -1; } }; final ObjectMetadata om = new ObjectMetadata(); om.setContentLength(0L); if (StringUtils.isNotBlank(serverSideEncryptionAlgorithm)) { om.setServerSideEncryption(serverSideEncryptionAlgorithm); } PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, im, om); putObjectRequest.setCannedAcl(cannedACL); s3.putObject(putObjectRequest); statistics.incrementWriteOps(1); }
@Override public PutObjectResult putObject(String bucketName, String key, String content) throws AmazonServiceException, SdkClientException { rejectNull(bucketName, "Bucket name must be provided"); rejectNull(key, "Object key must be provided"); rejectNull(content, "String content must be provided"); byte[] contentBytes = content.getBytes(StringUtils.UTF8); InputStream is = new ByteArrayInputStream(contentBytes); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentType("text/plain"); metadata.setContentLength(contentBytes.length); return putObject(new PutObjectRequest(bucketName, key, is, metadata)); }
/** * Constructs a new upload watcher and then immediately submits it to * the thread pool. * * @param manager * The {@link TransferManager} that owns this upload. * @param transfer * The transfer being processed. * @param threadPool * The {@link ExecutorService} to which we should submit new * tasks. * @param multipartUploadCallable * The callable responsible for processing the upload * asynchronously * @param putObjectRequest * The original putObject request * @param progressListenerChain * A chain of listeners that wish to be notified of upload * progress */ public static UploadMonitor create( TransferManager manager, UploadImpl transfer, ExecutorService threadPool, UploadCallable multipartUploadCallable, PutObjectRequest putObjectRequest, ProgressListenerChain progressListenerChain) { UploadMonitor uploadMonitor = new UploadMonitor(manager, transfer, threadPool, multipartUploadCallable, putObjectRequest, progressListenerChain); Future<UploadResult> thisFuture = threadPool.submit(uploadMonitor); // Use an atomic compareAndSet to prevent a possible race between the // setting of the UploadMonitor's futureReference, and setting the // CompleteMultipartUpload's futureReference within the call() method. // We only want to set the futureReference to UploadMonitor's futureReference if the // current value is null, otherwise the futureReference that's set is // CompleteMultipartUpload's which is ultimately what we want. uploadMonitor.futureReference.compareAndSet(null, thisFuture); return uploadMonitor; }
private PutObjectResult putObjectUsingMetadata(PutObjectRequest req) { ContentCryptoMaterial cekMaterial = createContentCryptoMaterial(req); // Wraps the object data with a cipher input stream final File fileOrig = req.getFile(); final InputStream isOrig = req.getInputStream(); PutObjectRequest wrappedReq = wrapWithCipher(req, cekMaterial); // Update the metadata req.setMetadata(updateMetadataWithContentCryptoMaterial( req.getMetadata(), req.getFile(), cekMaterial)); // Put the encrypted object into S3 try { return s3.putObject(wrappedReq); } finally { cleanupDataSource(req, fileOrig, isOrig, wrappedReq.getInputStream(), log); } }
/** * Stores a file in a previously created bucket. Downloads the file again and compares checksums * * @throws Exception if FileStreams can not be read */ @Test public void shouldUploadAndDownloadObject() throws Exception { final File uploadFile = new File(UPLOAD_FILE_NAME); s3Client.createBucket(BUCKET_NAME); s3Client.putObject(new PutObjectRequest(BUCKET_NAME, uploadFile.getName(), uploadFile)); final S3Object s3Object = s3Client.getObject(BUCKET_NAME, uploadFile.getName()); final InputStream uploadFileIS = new FileInputStream(uploadFile); final String uploadHash = HashUtil.getDigest(uploadFileIS); final String downloadedHash = HashUtil.getDigest(s3Object.getObjectContent()); uploadFileIS.close(); s3Object.close(); assertThat("Up- and downloaded Files should have equal Hashes", uploadHash, is(equalTo(downloadedHash))); }
/** * Tests if Object can be uploaded with KMS */ @Test public void shouldUploadWithEncryption() { final File uploadFile = new File(UPLOAD_FILE_NAME); final String objectKey = UPLOAD_FILE_NAME; s3Client.createBucket(BUCKET_NAME); final PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, objectKey, uploadFile); putObjectRequest.setSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(TEST_ENC_KEYREF)); s3Client.putObject(putObjectRequest); final GetObjectMetadataRequest getObjectMetadataRequest = new GetObjectMetadataRequest(BUCKET_NAME, objectKey); final ObjectMetadata objectMetadata = s3Client.getObjectMetadata(getObjectMetadataRequest); assertThat(objectMetadata.getContentLength(), is(uploadFile.length())); }
/** * Tests if Object can be uploaded with wrong KMS Key */ @Test public void shouldNotUploadStreamingWithWrongEncryptionKey() { final byte[] bytes = UPLOAD_FILE_NAME.getBytes(); final InputStream stream = new ByteArrayInputStream(bytes); final String objectKey = UUID.randomUUID().toString(); s3Client.createBucket(BUCKET_NAME); final ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(bytes.length); final PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, objectKey, stream, metadata); putObjectRequest.setSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(TEST_WRONG_KEYREF)); thrown.expect(AmazonS3Exception.class); thrown.expectMessage(containsString("Status Code: 400; Error Code: KMS.NotFoundException")); s3Client.putObject(putObjectRequest); }
/** * Puts an Object; Copies that object to a new bucket; Downloads the object from the new * bucket; compares checksums * of original and copied object * * @throws Exception if an Exception occurs */ @Test public void shouldCopyObject() throws Exception { final File uploadFile = new File(UPLOAD_FILE_NAME); final String sourceKey = UPLOAD_FILE_NAME; final String destinationBucketName = "destinationBucket"; final String destinationKey = "copyOf/" + sourceKey; final PutObjectResult putObjectResult = s3Client.putObject(new PutObjectRequest(BUCKET_NAME, sourceKey, uploadFile)); final CopyObjectRequest copyObjectRequest = new CopyObjectRequest(BUCKET_NAME, sourceKey, destinationBucketName, destinationKey); s3Client.copyObject(copyObjectRequest); final com.amazonaws.services.s3.model.S3Object copiedObject = s3Client.getObject(destinationBucketName, destinationKey); final String copiedHash = HashUtil.getDigest(copiedObject.getObjectContent()); copiedObject.close(); assertThat("Sourcefile and copied File should have same Hashes", copiedHash, is(equalTo(putObjectResult.getETag()))); }
/** * Puts an Object; Copies that object to a new bucket; Downloads the object from the new * bucket; compares checksums * of original and copied object * * @throws Exception if an Exception occurs */ @Test public void shouldCopyObjectEncrypted() throws Exception { final File uploadFile = new File(UPLOAD_FILE_NAME); final String sourceKey = UPLOAD_FILE_NAME; final String destinationBucketName = "destinationBucket"; final String destinationKey = "copyOf/" + sourceKey; s3Client.putObject(new PutObjectRequest(BUCKET_NAME, sourceKey, uploadFile)); final CopyObjectRequest copyObjectRequest = new CopyObjectRequest(BUCKET_NAME, sourceKey, destinationBucketName, destinationKey); copyObjectRequest.setSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(TEST_ENC_KEYREF)); final CopyObjectResult copyObjectResult = s3Client.copyObject(copyObjectRequest); final ObjectMetadata metadata = s3Client.getObjectMetadata(destinationBucketName, destinationKey); final InputStream uploadFileIS = new FileInputStream(uploadFile); final String uploadHash = HashUtil.getDigest(TEST_ENC_KEYREF, uploadFileIS); assertThat("ETag should match", copyObjectResult.getETag(), is(uploadHash)); assertThat("Files should have the same length", metadata.getContentLength(), is(uploadFile.length())); }
/** * Tests that an object wont be copied with wrong encryption Key * * @throws Exception if an Exception occurs */ @Test public void shouldNotObjectCopyWithWrongEncryptionKey() { final File uploadFile = new File(UPLOAD_FILE_NAME); final String sourceKey = UPLOAD_FILE_NAME; final String destinationBucketName = "destinationBucket"; final String destinationKey = "copyOf" + sourceKey; s3Client.putObject(new PutObjectRequest(BUCKET_NAME, sourceKey, uploadFile)); final CopyObjectRequest copyObjectRequest = new CopyObjectRequest(BUCKET_NAME, sourceKey, destinationBucketName, destinationKey); copyObjectRequest .setSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(TEST_WRONG_KEYREF)); thrown.expect(AmazonS3Exception.class); thrown.expectMessage(containsString("Status Code: 400; Error Code: KMS.NotFoundException")); s3Client.copyObject(copyObjectRequest); }
/** * Tests if the Metadata of an existing file can be retrieved. */ @Test public void shouldGetObjectMetadata() { final String nonExistingFileName = "nonExistingFileName"; final File uploadFile = new File(UPLOAD_FILE_NAME); s3Client.createBucket(BUCKET_NAME); final PutObjectResult putObjectResult = s3Client.putObject(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile)); final ObjectMetadata metadataExisting = s3Client.getObjectMetadata(BUCKET_NAME, UPLOAD_FILE_NAME); assertThat("The ETags should be identically!", metadataExisting.getETag(), is(putObjectResult.getETag())); thrown.expect(AmazonS3Exception.class); thrown.expectMessage(containsString("Status Code: 404")); s3Client.getObjectMetadata(BUCKET_NAME, nonExistingFileName); }
/** * Tests if the list objects can be retrieved. */ @Test public void shouldGetObjectListing() { final File uploadFile = new File(UPLOAD_FILE_NAME); s3Client.createBucket(BUCKET_NAME); s3Client.putObject(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile)); final ObjectListing objectListingResult = s3Client.listObjects(BUCKET_NAME, UPLOAD_FILE_NAME); assertThat("ObjectListinig has no S3Objects.", objectListingResult.getObjectSummaries().size(), is(greaterThan(0))); assertThat("The Name of the first S3ObjectSummary item has not expected the key name.", objectListingResult.getObjectSummaries().get(0).getKey(), is(UPLOAD_FILE_NAME)); }
/** * Tests if an object can be uploaded asynchronously * * @throws Exception not expected */ @Test public void shouldUploadInParallel() throws Exception { final File uploadFile = new File(UPLOAD_FILE_NAME); s3Client.createBucket(BUCKET_NAME); final TransferManager transferManager = createDefaultTransferManager(); final Upload upload = transferManager.upload(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile)); final UploadResult uploadResult = upload.waitForUploadResult(); assertThat(uploadResult.getKey(), equalTo(UPLOAD_FILE_NAME)); final S3Object getResult = s3Client.getObject(BUCKET_NAME, UPLOAD_FILE_NAME); assertThat(getResult.getKey(), equalTo(UPLOAD_FILE_NAME)); }
/** * Verify that range-downloads work. * * @throws Exception not expected */ @Test public void checkRangeDownloads() throws Exception { final File uploadFile = new File(UPLOAD_FILE_NAME); s3Client.createBucket(BUCKET_NAME); final TransferManager transferManager = createDefaultTransferManager(); final Upload upload = transferManager.upload(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile)); upload.waitForUploadResult(); final File downloadFile = File.createTempFile(UUID.randomUUID().toString(), null); transferManager .download(new GetObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME).withRange(1, 2), downloadFile) .waitForCompletion(); assertThat("Invalid file length", downloadFile.length(), is(2L)); transferManager .download(new GetObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME).withRange(0, 1000), downloadFile) .waitForCompletion(); assertThat("Invalid file length", downloadFile.length(), is(uploadFile.length())); }
/** * Stores a file in a previously created bucket. Downloads the file again and compares checksums. * * @throws Exception if FileStreams can not be read */ @Test public void shouldUploadAndDownloadObject() throws Exception { final File uploadFile = new File(UPLOAD_FILE_NAME); s3Client.createBucket(BUCKET_NAME); s3Client.putObject(new PutObjectRequest(BUCKET_NAME, uploadFile.getName(), uploadFile)); final S3Object s3Object = s3Client.getObject(BUCKET_NAME, uploadFile.getName()); final InputStream uploadFileIS = new FileInputStream(uploadFile); final String uploadHash = HashUtil.getDigest(uploadFileIS); final String downloadedHash = HashUtil.getDigest(s3Object.getObjectContent()); uploadFileIS.close(); s3Object.close(); assertThat("Up- and downloaded Files should have equal Hashes", uploadHash, is(equalTo(downloadedHash))); }
public static String webHookDump(InputStream stream, String school, String extension) { if (stream != null) { extension = extension == null || extension.isEmpty() ? ".xml" : extension.contains(".") ? extension : "." + extension; String fileName = "webhooks/" + school + "/" + school + "_" + Clock.getCurrentDateDashes() + "_" + Clock.getCurrentTime() + extension; AmazonS3 s3 = new AmazonS3Client(); Region region = Region.getRegion(Regions.US_WEST_2); s3.setRegion(region); try { File file = CustomUtilities.inputStreamToFile(stream); s3.putObject(new PutObjectRequest(name, fileName, file)); return CustomUtilities.fileToString(file); } catch (Exception e) { e.printStackTrace(); } } return ""; }
private void createEmptyObject(final String bucketName, final String objectName) throws AmazonClientException, AmazonServiceException { final InputStream im = new InputStream() { @Override public int read() throws IOException { return -1; } }; final ObjectMetadata om = new ObjectMetadata(); om.setContentLength(0L); if (StringUtils.isNotBlank(serverSideEncryptionAlgorithm)) { om.setSSEAlgorithm(serverSideEncryptionAlgorithm); } PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, im, om); putObjectRequest.setCannedAcl(cannedACL); s3.putObject(putObjectRequest); statistics.incrementWriteOps(1); }
public String uploadImageToS3(final BufferedImage image, final String fileKey) throws IOException { ByteArrayInputStream bis = null; final ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ImageIO.write(image, "png", bos); final byte[] bImageData = bos.toByteArray(); bis = new ByteArrayInputStream(bImageData); // upload to s3 bucket final PutObjectRequest s3Put = new PutObjectRequest(bucket, fileKey, bis, null).withCannedAcl(CannedAccessControlList.PublicRead); s3Client.putObject(s3Put); return getS3Url(fileKey); } finally { try { bos.close(); if (bis != null) bis.close(); } catch(IOException e) { logger.severe("Error while closing stream for writing an image for " + fileKey + " caused by " + e.getMessage()); } } }
protected static String uploadFileToS3(BufferedImage image, String word, Boolean codeOnly) throws IOException { ByteArrayInputStream bis = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { String bucket = SkillConfig.getS3BucketName(); String fileKey = getFileKey(word, codeOnly); ImageIO.write(image, "png", bos); byte[] bImageData = bos.toByteArray(); bis = new ByteArrayInputStream(bImageData); // upload to s3 bucket AWSCredentials awsCredentials = SkillConfig.getAWSCredentials(); AmazonS3Client s3Client = awsCredentials != null ? new AmazonS3Client(awsCredentials) : new AmazonS3Client(); PutObjectRequest s3Put = new PutObjectRequest(bucket, fileKey, bis, null).withCannedAcl(CannedAccessControlList.PublicRead); s3Client.putObject(s3Put); return getS3Url(word, codeOnly); } finally { bos.close(); if (bis != null) bis.close(); } }
public static void main(String[] args) throws Exception { // create the AWS S3 Client AmazonS3 s3 = AWSS3Factory.getS3Client(); // retrieve the object key and new object value from user System.out.println( "Enter the object key:" ); String key = new BufferedReader( new InputStreamReader( System.in ) ).readLine(); System.out.println( "Enter new object content:" ); String content = new BufferedReader( new InputStreamReader( System.in ) ).readLine(); // update the object in the demo bucket PutObjectRequest updateRequest = new PutObjectRequest(AWSS3Factory.S3_BUCKET, key, new StringInputStream(content), null); s3.putObject(updateRequest); // print out object key/value for validation System.out.println( String.format("update object [%s/%s] with new content: [%s]", AWSS3Factory.S3_BUCKET, key, content)); }
private void verifyStringPut(AmazonS3 mock, String key, String content) throws Exception { ArgumentCaptor<PutObjectRequest> argument = ArgumentCaptor.forClass(PutObjectRequest.class); verify(mock) .putObject(argument.capture()); PutObjectRequest req = argument.getValue(); assertEquals(key, req.getKey()); assertEquals(this.testBucket, req.getBucketName()); InputStreamReader input = new InputStreamReader(req.getInputStream(), "UTF-8"); StringBuilder sb = new StringBuilder(1024); final char[] buffer = new char[1024]; try { for(int read = input.read(buffer, 0, buffer.length); read != -1; read = input.read(buffer, 0, buffer.length)) { sb.append(buffer, 0, read); } } catch (IOException ignore) { } assertEquals(content, sb.toString()); }
@Override public void save(Note note) throws IOException { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setPrettyPrinting(); Gson gson = gsonBuilder.create(); String json = gson.toJson(note); String key = user + "/" + "notebook" + "/" + note.id() + "/" + "note.json"; File file = File.createTempFile("note", "json"); file.deleteOnExit(); Writer writer = new OutputStreamWriter(new FileOutputStream(file)); writer.write(json); writer.close(); s3client.putObject(new PutObjectRequest(bucketName, key, file)); }
@Test public void testPut() { ModelBucket bucket = getService(ModelBucket.class); InputStream stream = new ByteArrayInputStream("file content".getBytes()); ArgumentCaptor<PutObjectRequest> requestCaptor = ArgumentCaptor.forClass(PutObjectRequest.class); PutObjectResult expected = new PutObjectResult(); when(amazonS3Client.putObject(requestCaptor.capture())).thenReturn(expected); assertEquals(expected, bucket.put("path", stream, 12L)); PutObjectRequest request = requestCaptor.getValue(); assertEquals("model-bucket", request.getBucketName()); assertEquals("path", request.getKey()); assertEquals(stream, request.getInputStream()); assertEquals(12L, request.getMetadata().getContentLength()); List<Grant> grants = request.getAccessControlList().getGrantsAsList(); assertEquals(1, grants.size()); assertEquals(GroupGrantee.AllUsers, grants.get(0).getGrantee()); assertEquals(Permission.Read, grants.get(0).getPermission()); }