List of usage examples for com.amazonaws.services.s3.model ObjectMetadata ObjectMetadata
public ObjectMetadata()
From source file:com.eucalyptus.objectstorage.providers.s3.S3ProviderClient.java
License:Open Source License
@Override public InitiateMultipartUploadResponseType initiateMultipartUpload(InitiateMultipartUploadType request) throws S3Exception { InitiateMultipartUploadResponseType reply = request.getReply(); User requestUser = getRequestUser(request); OsgInternalS3Client internalS3Client = null; String bucketName = request.getBucket(); String key = request.getKey(); InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest( bucketName, key);/*from ww w. j a v a 2 s.c o m*/ ObjectMetadata metadata = new ObjectMetadata(); for (MetaDataEntry meta : request.getMetaData()) { metadata.addUserMetadata(meta.getName(), meta.getValue()); } initiateMultipartUploadRequest.setObjectMetadata(metadata); try { internalS3Client = getS3Client(requestUser); AmazonS3Client s3Client = internalS3Client.getS3Client(); InitiateMultipartUploadResult result = s3Client.initiateMultipartUpload(initiateMultipartUploadRequest); reply.setUploadId(result.getUploadId()); reply.setBucket(bucketName); reply.setKey(key); 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 w w w.j a v a 2 s . c o 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()); }// w w w. j a v a 2s. c om 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.flipzu.PostProcThread.java
License:Apache License
private boolean uploadToS3(Broadcast bcast, boolean delete) { debug.logPostProc("PostProcThread, S3 upload for " + bcast.getFilename()); if (bcast.getFilename() == null) { debug.logPostProc("PostProcThread, uploadToS3, filename is null"); return false; }// w ww .j av a 2 s .com File file = new File(bcast.getFilename()); if (!file.exists()) { debug.logPostProc("PostProcThread, uploadToS3, " + bcast.getFilename() + " does not exist"); return false; } AmazonS3 s3 = null; try { InputStream is = new FileInputStream("aws.properties"); s3 = new AmazonS3Client(new PropertiesCredentials(is)); } catch (Exception e) { Debug.getInstance().logError("uploadToS3 Error ", e); return false; } String bucketName = Config.getInstance().getS3Bucket(); String dirName = Config.getInstance().getS3dir(); String objName = dirName + "/" + bcast.getId() + Config.getInstance().getFileWriterExtension(); PutObjectRequest po = new PutObjectRequest(bucketName, objName, file); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentType("audio/mpeg"); po.setMetadata(metadata); po.setCannedAcl(CannedAccessControlList.PublicRead); try { s3.putObject(po); } catch (AmazonServiceException ase) { debug.logPostProc("Caught an AmazonServiceException, which means your request made it " + "to Amazon S3, but was rejected with an error response for some reason."); debug.logPostProc("Error Message: " + ase.getMessage()); debug.logPostProc("HTTP Status Code: " + ase.getStatusCode()); debug.logPostProc("AWS Error Code: " + ase.getErrorCode()); debug.logPostProc("Error Type: " + ase.getErrorType()); debug.logPostProc("Request ID: " + ase.getRequestId()); return false; } catch (AmazonClientException ace) { debug.logPostProc("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with S3, " + "such as not being able to access the network."); debug.logPostProc("Error Message: " + ace.getMessage()); return false; } if (delete) { if (Config.getInstance().deleteSmallBcasts()) /* check and remove empty/short broadcasts */ cleanCrappyBroadcasts(bcast.getKey(), file); debug.logPostProc("uploadToS3, deleting file " + bcast.getFilename()); file.delete(); } return true; }
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);//from w w w . ja v a 2s . co 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 {//from w ww . j a v a 2s.c o 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 w w w. j ava2 s . co 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/* ww w . j av a2s. co m*/ */ @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) {/*from ww w.ja v a 2 s. 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);/*w ww . j ava 2s . co 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); }