List of usage examples for com.amazonaws.services.s3.model ObjectMetadata ObjectMetadata
public ObjectMetadata()
From source file:org.restcomm.connect.commons.amazonS3.S3AccessTool.java
License:Open Source License
public URI uploadFile(final String fileToUpload) { if (s3client == null) { s3client = getS3client();//from w w w . ja v a 2 s . com } if (logger.isInfoEnabled()) { logger.info("S3 Region: " + bucketRegion.toString()); } try { if (testing && (!testingUrl.isEmpty() || !testingUrl.equals(""))) { // s3client.setEndpoint(testingUrl); // s3client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true)); FileUtils.touch(new File(URI.create(fileToUpload))); } StringBuffer bucket = new StringBuffer(); bucket.append(bucketName); if (folder != null && !folder.isEmpty()) bucket.append("/").append(folder); URI fileUri = URI.create(fileToUpload); if (logger.isInfoEnabled()) { logger.info("File to upload to S3: " + fileUri.toString()); } File file = new File(fileUri); while (!FileUtils.waitFor(file, 30)) { } if (file.exists()) { PutObjectRequest putRequest = new PutObjectRequest(bucket.toString(), file.getName(), file); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentType(new MimetypesFileTypeMap().getContentType(file)); putRequest.setMetadata(metadata); if (reducedRedundancy) putRequest.setStorageClass(StorageClass.ReducedRedundancy); s3client.putObject(putRequest); if (removeOriginalFile) { removeLocalFile(file); } URI recordingS3Uri = s3client.getUrl(bucket.toString(), file.getName()).toURI(); return recordingS3Uri; // return downloadUrl.toURI(); } else { logger.error("Timeout waiting for the recording file: " + file.getAbsolutePath()); return null; } } catch (AmazonServiceException ase) { logger.error("Caught an AmazonServiceException"); logger.error("Error Message: " + ase.getMessage()); logger.error("HTTP Status Code: " + ase.getStatusCode()); logger.error("AWS Error Code: " + ase.getErrorCode()); logger.error("Error Type: " + ase.getErrorType()); logger.error("Request ID: " + ase.getRequestId()); return null; } catch (AmazonClientException ace) { logger.error("Caught an AmazonClientException "); logger.error("Error Message: " + ace.getMessage()); return null; } catch (URISyntaxException e) { logger.error("URISyntaxException: " + e.getMessage()); return null; } catch (IOException e) { logger.error("Problem while trying to touch recording file for testing", e); return null; } }
From source file:org.serginho.awss3conn.Connection.java
public boolean updaloadFile(UploadedFile uploadedFile) { ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentLength(uploadedFile.getSize()); try {//from w w w . j ava 2s . c o m getS3Client().putObject(this.amazonBucket, this.key + uploadedFile.getFileName(), uploadedFile.getInputstream(), objectMetadata); } catch (IOException ex) { Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex); return false; } return true; }
From source file:org.springfield.lou.servlet.LouServlet.java
License:Open Source License
private String handleFileUpload(HttpServletRequest request) { System.out.println("HANDLE FILE UPLOAD"); try {//from w w w . j a va 2s . co m String targetid = request.getParameter("targetid"); System.out.println("TARGETID UPLOAD=" + targetid); String screenid = request.getParameter("screenid"); String cfilename = request.getParameter("cfilename"); System.out.println("CFILENAME=" + cfilename); String cfilesize = request.getParameter("cfilesize"); System.out.println("CFILESIZE=" + cfilesize); Html5ApplicationInterface app = null; String url = request.getRequestURI(); int pos = url.indexOf("/domain/"); if (pos != -1) { String tappname = url.substring(pos); app = ApplicationManager.instance().getApplication(tappname); } Screen eventscreen = app.getScreen(screenid); if (eventscreen == null) return null; String method = eventscreen.getModel() .getProperty("/screen['upload']/target['" + targetid + "']/method"); System.out.println("METHOD=" + method); String destpath = eventscreen.getModel() .getProperty("/screen['upload']/target['" + targetid + "']/destpath"); System.out.println("DESTPATH=" + destpath + " T=" + targetid); if (destpath == null || destpath.equals("")) { setUploadError(eventscreen, targetid, "destpath not set"); return null; } String destname_prefix = eventscreen.getModel() .getProperty("/screen['upload']/target['" + targetid + "']/destname_prefix"); if (destname_prefix == null || destname_prefix.equals("")) { setUploadError(eventscreen, targetid, "destname_prefix not set"); return null; } String filetype = eventscreen.getModel() .getProperty("/screen['upload']/target['" + targetid + "']/filetype"); if (filetype == null || filetype.equals("")) { setUploadError(eventscreen, targetid, "filetype not set"); return null; } String fileext = eventscreen.getModel() .getProperty("/screen['upload']/target['" + targetid + "']/fileext"); if (fileext == null || fileext.equals("")) { setUploadError(eventscreen, targetid, "fileext not set"); return null; } String checkupload = eventscreen.getModel() .getProperty("/screen['upload']/target['" + targetid + "']/checkupload"); if (checkupload == null || checkupload.equals("")) { setUploadError(eventscreen, targetid, "checkupload not set"); return null; } String storagehost = eventscreen.getModel() .getProperty("/screen['upload']/target['" + targetid + "']/storagehost"); if (storagehost == null || storagehost.equals("")) { setUploadError(eventscreen, targetid, "storagehost not set"); return null; } String destname_type = eventscreen.getModel() .getProperty("/screen['upload']/target['" + targetid + "']/destname_type"); if (destname_type == null || destname_type.equals("")) { setUploadError(eventscreen, targetid, "destname_type not set"); return null; } String publicpath = eventscreen.getModel() .getProperty("/screen['upload']/target['" + targetid + "']/publicpath"); if (publicpath == null || publicpath.equals("")) { setUploadError(eventscreen, targetid, "publicpath not set"); return null; } // here we can check if its a valid upload based on filename and other specs and kill if needed, also map real extension fileext = getValidExtension(fileext, cfilename); if (fileext == null) return null; // kill the request its not a valid format if (method.equals("s3amazon")) { String bucketname = eventscreen.getModel() .getProperty("/screen['upload']/target['" + targetid + "']/bucketname"); if (bucketname == null || bucketname.equals("")) { setUploadError(eventscreen, targetid, "bucketname not set"); return null; } AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new EnvironmentVariableCredentialsProvider()).build(); String filename = "unknown"; int storageport = 22; if (destname_type.equals("epoch")) { filename = destpath + destname_prefix + "" + new Date().getTime(); } String publicurl = publicpath + bucketname + "/" + filename + "." + fileext; FsPropertySet ps = new FsPropertySet(); // we will use this to send status reports back ps.setProperty("action", "start"); ps.setProperty("progress", "0"); ps.setProperty("cfilename", cfilename); ps.setProperty("url", publicurl); eventscreen.getModel().setProperties("/screen/upload/" + targetid, ps); try { InputStream inst = request.getInputStream(); int read = 0; int readtotal = 0; int b; while ((b = inst.read()) != 44) { // skip the base64 tagline, not sure how todo this better } Base64InputStream b64i = new Base64InputStream(inst); //System.out.println("Uploading a new object to S3 from a stream "+bucketname+"/"+filename+"."+fileext); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentType(filetype + "/" + fileext); PutObjectRequest or = new PutObjectRequest(bucketname, filename + "." + fileext, b64i, metadata); or.setGeneralProgressListener(new UploadProgressListener(eventscreen.getModel(), publicurl, cfilename, cfilesize, targetid)); s3Client.putObject(or); } catch (AmazonServiceException ase) { ase.printStackTrace(); } ps.setProperty("action", "done"); ps.setProperty("progress", "100"); ps.setProperty("cfilename", cfilename); ps.setProperty("url", publicurl); eventscreen.getModel().setProperties("/screen/upload/" + targetid, ps); return bucketname + "/" + filename + "." + fileext; } else if (method.equals("scp")) { String pemfile = eventscreen.getModel() .getProperty("/screen['upload']/target['" + targetid + "']/pemfile"); if (destpath == null || destpath.equals("")) { setUploadError(eventscreen, targetid, "destpath not set"); return null; } String storagename = eventscreen.getModel() .getProperty("/screen['upload']/target['" + targetid + "']/storagename"); if (storagename == null || storagehost.equals("")) { setUploadError(eventscreen, targetid, "storagename not set"); return null; } String filename = "unknown"; int storageport = 22; if (destname_type.equals("epoch")) { filename = destname_prefix + "" + new Date().getTime(); } String publicurl = publicpath + filename + "." + fileext; FsPropertySet ps = new FsPropertySet(); // we will use this to send status reports back ps.setProperty("action", "start"); ps.setProperty("progress", "0"); ps.setProperty("url", publicurl); eventscreen.getModel().setProperties("/screen/upload/" + targetid, ps); JSch jsch = new JSch(); jsch.addIdentity(pemfile); jsch.setConfig("StrictHostKeyChecking", "no"); Session session = jsch.getSession(storagename, storagehost, storageport); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp channelSftp = (ChannelSftp) channel; channelSftp.cd(destpath); InputStream inst = request.getInputStream(); int read = 0; int readtotal = 0; int b; while ((b = inst.read()) != 44) { // skip the base64 tagline, not sure how todo this better } Base64InputStream b64i = new Base64InputStream(inst); channelSftp.put(b64i, filename + "." + fileext); ps.setProperty("action", "done"); ps.setProperty("progress", "100"); ps.setProperty("url", publicurl); eventscreen.getModel().setProperties("/screen/upload/" + targetid, ps); return filename + "." + fileext; } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.springframework.build.aws.maven.PrivateS3Wagon.java
License:Apache License
private PutObjectRequest createDirectoryPutObjectRequest(String key) { ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]); ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentLength(0);//from w w w. j a v a 2 s .c o m return new PutObjectRequest(this.bucketName, key, inputStream, objectMetadata); }
From source file:org.springframework.integration.aws.outbound.S3MessageHandler.java
License:Apache License
private Transfer upload(Message<?> requestMessage) { Object payload = requestMessage.getPayload(); String bucketName = obtainBucket(requestMessage); String key = null;/*w w w. j a v a 2s. c o m*/ if (this.keyExpression != null) { key = this.keyExpression.getValue(this.evaluationContext, requestMessage, String.class); } if (payload instanceof File && ((File) payload).isDirectory()) { File fileToUpload = (File) payload; if (key == null) { key = fileToUpload.getName(); } return this.transferManager.uploadDirectory(bucketName, key, fileToUpload, true, new MessageHeadersObjectMetadataProvider(requestMessage.getHeaders())); } else { ObjectMetadata metadata = new ObjectMetadata(); if (this.uploadMetadataProvider != null) { this.uploadMetadataProvider.populateMetadata(metadata, requestMessage); } InputStream inputStream; if (payload instanceof InputStream) { inputStream = (InputStream) payload; } else if (payload instanceof File) { File fileToUpload = (File) payload; if (key == null) { key = fileToUpload.getName(); } try { inputStream = new FileInputStream(fileToUpload); if (metadata.getContentLength() == 0) { metadata.setContentLength(fileToUpload.length()); } if (metadata.getContentType() == null) { metadata.setContentType(Mimetypes.getInstance().getMimetype(fileToUpload)); } } catch (FileNotFoundException e) { throw new AmazonClientException(e); } } else if (payload instanceof byte[]) { inputStream = new ByteArrayInputStream((byte[]) payload); } else { throw new IllegalArgumentException("Unsupported payload type: [" + payload.getClass() + "]. The only supported payloads for the upload request are " + "java.io.File, java.io.InputStream, byte[] and PutObjectRequest."); } Assert.state(key != null, "The 'keyExpression' must not be null for non-File payloads and can't evaluate to null. " + "Root object is: " + requestMessage); if (key == null) { if (this.keyExpression != null) { throw new IllegalStateException( "The 'keyExpression' [" + this.keyExpression.getExpressionString() + "] must not evaluate to null. Root object is: " + requestMessage); } else { throw new IllegalStateException("Specify a 'keyExpression' for non-java.io.File payloads"); } } if (metadata.getContentMD5() == null) { String contentMd5 = null; try { contentMd5 = Md5Utils.md5AsBase64(StreamUtils.copyToByteArray(inputStream)); if (inputStream.markSupported()) { inputStream.reset(); } metadata.setContentMD5(contentMd5); } catch (IOException e) { throw new MessageHandlingException(requestMessage, e); } } PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, metadata); S3ProgressListener progressListener = this.s3ProgressListener; if (this.objectAclExpression != null) { Object acl = this.objectAclExpression.getValue(this.evaluationContext, requestMessage); Assert.state(acl instanceof AccessControlList || acl instanceof CannedAccessControlList, "The 'objectAclExpression' [" + this.objectAclExpression.getExpressionString() + "] must evaluate to com.amazonaws.services.s3.model.AccessControlList " + "or must evaluate to com.amazonaws.services.s3.model.CannedAccessControlList. " + "Gotten: [" + acl + "]"); SetObjectAclRequest aclRequest; if (acl instanceof AccessControlList) { aclRequest = new SetObjectAclRequest(bucketName, key, (AccessControlList) acl); } else { aclRequest = new SetObjectAclRequest(bucketName, key, (CannedAccessControlList) acl); } final SetObjectAclRequest theAclRequest = aclRequest; progressListener = new S3ProgressListener() { @Override public void onPersistableTransfer(PersistableTransfer persistableTransfer) { } @Override public void progressChanged(ProgressEvent progressEvent) { if (ProgressEventType.TRANSFER_COMPLETED_EVENT.equals(progressEvent.getEventType())) { S3MessageHandler.this.transferManager.getAmazonS3Client().setObjectAcl(theAclRequest); } } }; if (this.s3ProgressListener != null) { progressListener = new S3ProgressListenerChain(this.s3ProgressListener, progressListener); } } if (progressListener != null) { return this.transferManager.upload(putObjectRequest, progressListener); } else { return this.transferManager.upload(putObjectRequest); } } }
From source file:org.springframework.integration.aws.s3.core.AmazonS3OperationsImpl.java
License:Apache License
public void putObject(String bucketName, String folder, String objectName, AmazonS3Object s3Object) { if (logger.isDebugEnabled()) { logger.debug("Putting object to bucket " + bucketName + " and folder " + folder); logger.debug("Object Name is " + objectName); }/*from w w w . j a v a 2s. c om*/ if (objectName == null) throw new AmazonS3OperationException(credentials.getAccessKey(), bucketName, objectName, "Object Name is Mandatory"); boolean isTempFile = false; File file = s3Object.getFileSource(); InputStream in = s3Object.getInputStream(); if (file != null && in != null) throw new AmazonS3OperationException(credentials.getAccessKey(), bucketName, objectName, "File Object and Input Stream in the S3 Object are mutually exclusive"); if (file == null && in == null) throw new AmazonS3OperationException(credentials.getAccessKey(), bucketName, objectName, "At lease one of File object or Input Stream in the S3 Object are mandatory"); String key; if (folder != null) { key = folder.endsWith(PATH_SEPARATOR) ? folder + objectName : folder + PATH_SEPARATOR + objectName; } else { key = objectName; } if (in != null) { file = getTempFile(in, bucketName, objectName); isTempFile = true; } PutObjectRequest request; if (file != null) { request = new PutObjectRequest(bucketName, key, file); //if the size of the file is greater than the threshold for multipart upload, //set the Content-MD5 header for this upload. This header will also come handy //later in inbound-channel-adapter where we cant find the MD5 sum of the //multipart upload file from its ETag String stringContentMD5 = null; try { stringContentMD5 = AmazonWSCommonUtils.encodeHex(AmazonWSCommonUtils.getContentsMD5AsBytes(file)); } catch (UnsupportedEncodingException e) { logger.error("Exception while generating the content's MD5 of the file " + file.getAbsolutePath(), e); } if (stringContentMD5 != null) { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentMD5(stringContentMD5); request.withMetadata(metadata); } } else throw new AmazonS3OperationException(credentials.getAccessKey(), bucketName, objectName, "Unable to get the File handle to upload the file to S3"); Upload upload; try { upload = transferManager.upload(request); } catch (Exception e) { throw new AmazonS3OperationException(credentials.getAccessKey(), bucketName, objectName, "Encountered Exception while invoking upload on multipart/single thread file, " + "see nested exceptions for more details", e); } //Wait till the upload completes, the call to putObject is synchronous try { if (logger.isInfoEnabled()) logger.info("Waiting for Upload to complete"); upload.waitForCompletion(); if (logger.isInfoEnabled()) logger.info("Upload completed"); } catch (Exception e) { throw new AmazonS3OperationException(credentials.getAccessKey(), bucketName, objectName, "Encountered Exception while uploading the multipart/single thread file, " + "see nested exceptions for more details", e); } if (isTempFile) { //Delete the temp file if (logger.isDebugEnabled()) logger.debug("Deleting temp file: " + file.getName()); file.delete(); } //Now since the object is present on S3, set the AccessControl list on it //Please note that it is not possible to set the object ACL with the //put object request, and hence both these operations cannot be atomic //it is possible the objects is uploaded and the ACl not set due to some //failure AmazonS3ObjectACL acl = s3Object.getObjectACL(); AccessControlList objectACL = getAccessControlList(bucketName, key, acl); if (objectACL != null) { if (logger.isInfoEnabled()) logger.info("Setting Access control list for key " + key); try { client.setObjectAcl(bucketName, key, objectACL); } catch (Exception e) { throw new AmazonS3OperationException(credentials.getAccessKey(), bucketName, objectName, "Encountered Exception while setting the Object ACL for key , " + key + "see nested exceptions for more details", e); } if (logger.isDebugEnabled()) logger.debug("Successfully set the object ACL"); } else { if (logger.isInfoEnabled()) logger.info("No Object ACL found to be set"); } }
From source file:org.springframework.integration.aws.s3.core.DefaultAmazonS3Operations.java
License:Apache License
/** * The implementation puts the given {@link File} instance to the provided bucket against * the given key./*from w w w. j a v a 2 s . c o m*/ * * @param bucketName The bucket on S3 where this object is to be put * @param key The key against which this Object is to be stored in S3 * @param file resource to be uploaded to S3 * @param objectACL the Object's Access controls for the object to be uploaded * @param userMetadata The user's metadata to be associated with the object uploaded * @param stringContentMD5 The MD5 sum of the contents of the file to be uploaded */ @Override public void doPut(String bucketName, String key, File file, AmazonS3ObjectACL objectACL, Map<String, String> userMetadata, String stringContentMD5) { ObjectMetadata metadata = new ObjectMetadata(); PutObjectRequest request = new PutObjectRequest(bucketName, key, file); request.withMetadata(metadata); if (stringContentMD5 != null) { metadata.setContentMD5(stringContentMD5); } if (userMetadata != null) { metadata.setUserMetadata(userMetadata); } Upload upload; try { upload = transferManager.upload(request); } catch (Exception e) { throw new AmazonS3OperationException(credentials.getAccessKey(), bucketName, key, "Encountered Exception while invoking upload on multipart/single thread file, " + "see nested exceptions for more details", e); } //Wait till the upload completes, the call to putObject is synchronous try { if (logger.isInfoEnabled()) { logger.info("Waiting for Upload to complete"); } upload.waitForCompletion(); if (logger.isInfoEnabled()) { logger.info("Upload completed"); } } catch (Exception e) { throw new AmazonS3OperationException(credentials.getAccessKey(), bucketName, key, "Encountered Exception while uploading the multipart/single thread file, " + "see nested exceptions for more details", e); } //Now since the object is present on S3, set the AccessControl list on it //Please note that it is not possible to set the object ACL with the //put object request, and hence both these operations cannot be atomic //it is possible the objects is uploaded and the ACl not set due to some //failure if (objectACL != null) { if (logger.isInfoEnabled()) { logger.info("Setting Access control list for key " + key); } try { client.setObjectAcl(bucketName, key, getAccessControlList(bucketName, key, objectACL)); } catch (Exception e) { throw new AmazonS3OperationException(credentials.getAccessKey(), bucketName, key, "Encountered Exception while setting the Object ACL for key , " + key + "see nested exceptions for more details", e); } if (logger.isDebugEnabled()) { logger.debug("Successfully set the object ACL"); } } }
From source file:org.springframework.integration.aws.support.S3Session.java
License:Apache License
@Override public void write(InputStream inputStream, String destination) throws IOException { Assert.notNull(inputStream, "'inputStream' must not be null."); String[] bucketKey = splitPathToBucketAndKey(destination); this.amazonS3.putObject(bucketKey[0], bucketKey[1], inputStream, new ObjectMetadata()); }
From source file:org.symphonyoss.vb.util.AwsS3Client.java
License:Apache License
public void putObject(String destBucket, String key, InputStream inputStream, ObjectMetadata metaData) { try {//from www .j a va 2s . co m logger.info("Put object for s3://{}/{}", destBucket, key); byte[] bytes = IOUtils.toByteArray(inputStream); if (metaData == null) metaData = new ObjectMetadata(); metaData.setContentLength(bytes.length); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); s3Client.putObject(new PutObjectRequest(destBucket, key, byteArrayInputStream, metaData)); } catch (AmazonServiceException ase) { logger.error("Caught an AmazonServiceException, " + "which means your request made it " + "to Amazon S3, but was rejected with an error response " + "for some reason."); logger.error("Error Message: " + ase.getMessage()); logger.error("HTTP Status Code: " + ase.getStatusCode()); logger.error("AWS Error Code: " + ase.getErrorCode()); logger.error("Error Type: " + ase.getErrorType()); logger.error("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { logger.error("Caught an AmazonClientException, " + "which means the client encountered " + "an internal error while trying to communicate" + " with S3, " + "such as not being able to access the network."); logger.error("Error Message: " + ace.getMessage()); } catch (IOException e) { logger.error("Obtaining length", e); } }
From source file:org.weakref.s3fs.util.AmazonS3ClientMock.java
License:Apache License
private S3Element parse(ByteArrayInputStream stream, String bucket, String key) { S3Object object = new S3Object(); object.setBucketName(bucket);/*from w ww. j a va 2 s . c o m*/ object.setKey(key); ObjectMetadata metadata = new ObjectMetadata(); metadata.setLastModified(new Date()); metadata.setContentLength(stream.available()); object.setObjectContent(stream); object.setObjectMetadata(metadata); // TODO: create converter between path permission and s3 permission AccessControlList permission = createAllPermission(); return new S3Element(object, permission, false); }