List of usage examples for com.amazonaws.services.s3.model PutObjectRequest PutObjectRequest
public PutObjectRequest(String bucketName, String key, InputStream input, ObjectMetadata metadata)
From source file:com.eucalyptus.objectstorage.providers.s3.S3ProviderClient.java
License:Open Source License
@Override public PutObjectResponseType putObject(PutObjectType request, InputStream inputData) throws S3Exception { User requestUser = getRequestUser(request); OsgInternalS3Client internalS3Client = null; try {//from ww w .ja v a 2 s . c o m internalS3Client = getS3Client(requestUser); AmazonS3Client s3Client = internalS3Client.getS3Client(); PutObjectResult result; ObjectMetadata metadata = getS3ObjectMetadata(request); //Set the acl to private. PutObjectRequest putRequest = new PutObjectRequest(request.getBucket(), request.getKey(), inputData, metadata).withCannedAcl(CannedAccessControlList.Private); result = s3Client.putObject(putRequest); PutObjectResponseType reply = request.getReply(); if (result == null) { throw new InternalErrorException("Null result from backend"); } else { reply.setEtag(result.getETag()); reply.setVersionId(result.getVersionId()); reply.setLastModified(new Date()); } return reply; } catch (AmazonServiceException e) { LOG.debug("Error from backend", e); throw S3ExceptionMapper.fromAWSJavaSDK(e); } }
From source file:com.eucalyptus.portal.BucketUploadableActivities.java
License:Open Source License
protected boolean upload(final String accountId, final String keyName, InputStream contents) throws S3UploadException { Optional<String> bucketName; try {/*from www . ja v a2s.co m*/ bucketName = this.billingInfos.lookupByAccount(accountId, AccountFullName.getInstance(accountId), (info) -> info.getBillingReportsBucket() != null ? Optional.of(info.getBillingReportsBucket()) : Optional.empty()); } catch (final Exception ex) { throw new S3UploadException("Failed to lookup user's bucket setting"); } if (bucketName.isPresent()) { try { final EucaS3Client s3c = getS3Client(); // this will throw error if bucket policy does not allow billing writing into the bucket if (s3c != null) { final PutObjectRequest req = new PutObjectRequest(bucketName.get(), keyName, contents, new ObjectMetadata()).withCannedAcl(CannedAccessControlList.BucketOwnerFullControl); s3c.putObject(req); return true; } } catch (final AmazonServiceException ex) { throw new S3UploadException("Failed to upload due to S3 service error: " + ex.getErrorCode()); } catch (final SdkClientException ex) { throw new S3UploadException("Failed to upload due to S3 client error", ex); } catch (final Exception ex) { throw new S3UploadException("Failed to upload report to bucket", ex); } } return false; }
From source file:com.eucalyptus.portal.PortalService.java
License:Open Source License
public ModifyBillingResponseType modifyBilling(final ModifyBillingType request) throws PortalServiceException { final Context context = checkAuthorized(); final ModifyBillingResponseType response = request.getReply(); Function<BillingInfo, BillingInfo> updater = info -> { info.setBillingReportsBucket(request.getReportBucket()); info.setDetailedBillingEnabled(MoreObjects.firstNonNull(request.getDetailedBillingEnabled(), false)); if (request.getActiveCostAllocationTags() != null) { info.setActiveCostAllocationTags(request.getActiveCostAllocationTags()); }//from w w w.ja v a2 s . co m return info; }; final Predicate<String> testBucket = (bucket) -> { try { final EucaS3Client s3c = BucketUploadableActivities.getS3Client(); PutObjectRequest req = new PutObjectRequest(bucket, "aws-programmatic-access-test-object", new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8)), new ObjectMetadata()) .withCannedAcl(CannedAccessControlList.BucketOwnerFullControl); s3c.putObject(req); return true; } catch (final Exception ex) { ; } return false; }; try { if (request.getReportBucket() != null && !testBucket.test(request.getReportBucket())) { throw new PortalInvalidParameterException("Requested bucket is not accessible by billing"); } try { response.getResult().setBillingSettings( billingInfos.updateByAccount(context.getAccountNumber(), context.getAccount(), info -> TypeMappers.transform(updater.apply(info), BillingSettings.class))); } catch (PortalMetadataNotFoundException e) { final BillingInfo billingInfo = updater.apply(billingInfos.defaults()); billingInfo.setOwner(context.getUserFullName()); billingInfo.setDisplayName(context.getAccountNumber()); response.getResult().setBillingSettings(billingInfos.save(billingInfo, TypeMappers.lookupF(BillingInfo.class, BillingSettings.class))); } } catch (Exception e) { throw handleException(e); } return response; }
From source file:com.formkiq.core.service.AssetServiceS3Default.java
License:Apache License
@Override public void createFolder(final String folder) { // create meta-data for your folder and set content-length to 0 ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(0);// www .ja va 2 s. c o m // create empty content InputStream emptyContent = new ByteArrayInputStream(new byte[0]); // create a PutObjectRequest passing the folder name suffixed by / PutObjectRequest putObjectRequest = new PutObjectRequest(this.s3BucketName, folder + "/", emptyContent, metadata); // send request to S3 to create folder getS3Connection().putObject(putObjectRequest); }
From source file:com.formkiq.core.service.AssetServiceS3Default.java
License:Apache License
@Override public void saveAsset(final String folder, final String asset, final byte[] bytes) { ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(bytes.length); metadata.setContentType("application/zip"); try {/* ww w . ja va 2s . co m*/ String filename = getFilename(folder, asset); AmazonS3 connection = getS3Connection(); PutObjectRequest req = new PutObjectRequest(this.s3BucketName, filename, bis, metadata); connection.putObject(req); LOG.log(Level.FINE, "saving " + filename + " to " + S3_BUCKET_NAME); } finally { IOUtils.closeQuietly(bis); } }
From source file:com.ge.predix.sample.blobstore.repository.BlobstoreService.java
License:Apache License
/** * Adds a new Blob to the binded bucket in the Object Store * * @param obj S3Object to be added/*from www .ja v a2s. c o m*/ * @throws Exception */ public void put(S3Object obj) throws Exception { if (obj == null) { log.error("put(): Empty file provided"); throw new Exception("File is null"); } InputStream is = obj.getObjectContent(); List<PartETag> partETags = new ArrayList<>(); InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucket, obj.getKey()); InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest); try { int i = 1; int currentPartSize = 0; ByteArrayOutputStream tempBuffer = new ByteArrayOutputStream(); int byteValue; while ((byteValue = is.read()) != -1) { tempBuffer.write(byteValue); currentPartSize = tempBuffer.size(); if (currentPartSize == (50 * 1024 * 1024)) //make this a const { byte[] b = tempBuffer.toByteArray(); ByteArrayInputStream byteStream = new ByteArrayInputStream(b); UploadPartRequest uploadPartRequest = new UploadPartRequest().withBucketName(bucket) .withKey(obj.getKey()).withUploadId(initResponse.getUploadId()).withPartNumber(i++) .withInputStream(byteStream).withPartSize(currentPartSize); partETags.add(s3Client.uploadPart(uploadPartRequest).getPartETag()); tempBuffer.reset(); } } log.info("currentPartSize: " + currentPartSize); ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentLength(currentPartSize); obj.setObjectMetadata(objectMetadata); if (i == 1 && currentPartSize < (5 * 1024 * 1024)) // make this a const { s3Client.abortMultipartUpload( new AbortMultipartUploadRequest(bucket, obj.getKey(), initResponse.getUploadId())); byte[] b = tempBuffer.toByteArray(); ByteArrayInputStream byteStream = new ByteArrayInputStream(b); objectMetadata.setContentType(getContentType(b)); obj.setObjectMetadata(objectMetadata); PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, obj.getKey(), byteStream, obj.getObjectMetadata()); s3Client.putObject(putObjectRequest); return; } if (currentPartSize > 0 && currentPartSize <= (50 * 1024 * 1024)) // make this a const { byte[] b = tempBuffer.toByteArray(); ByteArrayInputStream byteStream = new ByteArrayInputStream(b); log.info("currentPartSize: " + currentPartSize); log.info("byteArray: " + b); UploadPartRequest uploadPartRequest = new UploadPartRequest().withBucketName(bucket) .withKey(obj.getKey()).withUploadId(initResponse.getUploadId()).withPartNumber(i) .withInputStream(byteStream).withPartSize(currentPartSize); partETags.add(s3Client.uploadPart(uploadPartRequest).getPartETag()); } } catch (Exception e) { log.error("put(): Exception occurred in put(): " + e.getMessage()); s3Client.abortMultipartUpload( new AbortMultipartUploadRequest(bucket, obj.getKey(), initResponse.getUploadId())); throw e; } CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest() .withBucketName(bucket).withPartETags(partETags).withUploadId(initResponse.getUploadId()) .withKey(obj.getKey()); s3Client.completeMultipartUpload(completeMultipartUploadRequest); }
From source file:com.ge.predix.solsvc.blobstore.bootstrap.BlobstoreClientImpl.java
License:Apache License
/** * Adds a new Blob to the binded bucket in the Object Store * * @param obj S3Object to be added//from w ww. j a v a 2 s. c om */ @Override public String saveBlob(S3Object obj) { if (obj == null) { this.log.error("put(): Empty file provided"); //$NON-NLS-1$ throw new RuntimeException("File is null"); //$NON-NLS-1$ } List<PartETag> partETags = new ArrayList<>(); String bucket = this.blobstoreConfig.getBucketName(); InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucket, obj.getKey()); InitiateMultipartUploadResult initResponse = this.s3Client.initiateMultipartUpload(initRequest); try (InputStream is = obj.getObjectContent();) { int i = 1; int currentPartSize = 0; ByteArrayOutputStream tempBuffer = new ByteArrayOutputStream(); int byteValue; while ((byteValue = is.read()) != -1) { tempBuffer.write(byteValue); currentPartSize = tempBuffer.size(); if (currentPartSize == (50 * 1024 * 1024)) //make this a const { byte[] b = tempBuffer.toByteArray(); ByteArrayInputStream byteStream = new ByteArrayInputStream(b); UploadPartRequest uploadPartRequest = new UploadPartRequest().withBucketName(bucket) .withKey(obj.getKey()).withUploadId(initResponse.getUploadId()).withPartNumber(i++) .withInputStream(byteStream).withPartSize(currentPartSize); partETags.add(this.s3Client.uploadPart(uploadPartRequest).getPartETag()); tempBuffer.reset(); } } this.log.info("currentPartSize: " + currentPartSize); //$NON-NLS-1$ ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentLength(currentPartSize); if (this.enableSSE) { objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); } obj.setObjectMetadata(objectMetadata); if (i == 1 && currentPartSize < (5 * 1024 * 1024)) // make this a const { this.s3Client.abortMultipartUpload( new AbortMultipartUploadRequest(bucket, obj.getKey(), initResponse.getUploadId())); byte[] b = tempBuffer.toByteArray(); ByteArrayInputStream byteStream = new ByteArrayInputStream(b); objectMetadata.setContentType(getContentType(b)); if (this.enableSSE) { objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); } obj.setObjectMetadata(objectMetadata); PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, obj.getKey(), byteStream, obj.getObjectMetadata()); this.s3Client.putObject(putObjectRequest); ObjectMetadata meta = this.s3Client.getObjectMetadata(bucket, obj.getKey()); Map<String, Object> headers = meta.getRawMetadata(); for (Map.Entry<String, Object> entry : headers.entrySet()) { this.log.info("Object Metadata -- " + entry.getKey() + ": " + entry.getValue().toString()); //$NON-NLS-1$ //$NON-NLS-2$ } return initResponse.getUploadId(); } if (currentPartSize > 0 && currentPartSize <= (50 * 1024 * 1024)) // make this a const { byte[] b = tempBuffer.toByteArray(); ByteArrayInputStream byteStream = new ByteArrayInputStream(b); this.log.info("currentPartSize: " + currentPartSize); //$NON-NLS-1$ this.log.info("byteArray: " + b); //$NON-NLS-1$ UploadPartRequest uploadPartRequest = new UploadPartRequest().withBucketName(bucket) .withKey(obj.getKey()).withUploadId(initResponse.getUploadId()).withPartNumber(i) .withInputStream(byteStream).withPartSize(currentPartSize); partETags.add(this.s3Client.uploadPart(uploadPartRequest).getPartETag()); } CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest() .withBucketName(bucket).withPartETags(partETags).withUploadId(initResponse.getUploadId()) .withKey(obj.getKey()); this.s3Client.completeMultipartUpload(completeMultipartUploadRequest); return initResponse.getUploadId(); } catch (Exception e) { this.log.error("put(): Exception occurred in put(): " + e.getMessage()); //$NON-NLS-1$ this.s3Client.abortMultipartUpload( new AbortMultipartUploadRequest(bucket, obj.getKey(), initResponse.getUploadId())); throw new RuntimeException("put(): Exception occurred in put(): ", e); //$NON-NLS-1$ } }
From source file:com.gendevs.bedrock.appengine.service.storage.StorageProvider.java
License:Apache License
private String uploadFile(InputStream imageInputStream, String contentType, String organisationId, String id, String type) {/* w w w .j a v a 2s.c o m*/ initalizeS3(); String fileName = null; switch (type) { case "app": fileName = String.format(StorageConstants.APP_PROFILE_PHOTOS, organisationId, id); break; case "user": fileName = String.format(StorageConstants.USER_PROFILE_PHOTOS, organisationId, id); break; } ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentType(contentType); PutObjectRequest por = new PutObjectRequest(bucketName, fileName, imageInputStream, metadata); por.setCannedAcl(CannedAccessControlList.PublicRead); Upload upload = manager.upload(por); try { upload.waitForCompletion(); } catch (AmazonServiceException e) { e.printStackTrace(); } catch (AmazonClientException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return fileName; }
From source file:com.github.abhinavmishra14.aws.s3.service.impl.AwsS3IamServiceImpl.java
License:Open Source License
@Override public PutObjectResult createDirectory(final String bucketName, final String dirName, final CannedAccessControlList cannedAcl) throws AmazonClientException, AmazonServiceException { LOGGER.info("createDirectory invoked, bucketName: {}, dirName: {} and cannedAccessControlList: {}", bucketName, dirName, cannedAcl); final ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(0);/*from w w w . j av a2 s. c o m*/ // Create empty content,since creating empty folder needs an empty content final InputStream emptyContent = new ByteArrayInputStream(new byte[0]); // Create a PutObjectRequest passing the directory name suffixed by '/' final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, dirName + AWSUtilConstants.SEPARATOR, emptyContent, metadata).withCannedAcl(cannedAcl); return s3client.putObject(putObjectRequest); }
From source file:com.github.abhinavmishra14.aws.s3.service.impl.AwsS3IamServiceImpl.java
License:Open Source License
@Override public PutObjectResult createDirectory(final String bucketName, final String dirName, final boolean isPublicAccessible) throws AmazonClientException, AmazonServiceException { LOGGER.info("createDirectory invoked, bucketName: {}, dirName: {} and isPublicAccessible: {}", bucketName, dirName, isPublicAccessible); final ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(0);/*from w w w . j a va2s . c o m*/ // Create empty content,since creating empty folder needs an empty content final InputStream emptyContent = new ByteArrayInputStream(new byte[0]); // Create a PutObjectRequest passing the directory name suffixed by '/' final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, dirName + AWSUtilConstants.SEPARATOR, emptyContent, metadata); if (isPublicAccessible) { putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead); } return s3client.putObject(putObjectRequest); }