List of usage examples for com.amazonaws.services.s3.model GeneratePresignedUrlRequest GeneratePresignedUrlRequest
public GeneratePresignedUrlRequest(String bucketName, String key)
From source file:org.cyclopsgroup.cym2.awss3.S3PresignedUrlTransferListener.java
License:Apache License
/** * Generates a presigned URL for the specified resource. * * @param resource the resource// www . j a v a2s . c o m * @param expiration the expiration date * @return a presigned URL */ private URL generatePresignedUrl(final Resource resource, final Date expiration) { assert (resource != null); assert (expiration != null); final String bucketName = this.wagon.getBucketName(); final String key = this.wagon.getKeyPrefix() + resource.getName(); return this.wagon.getS3() .generatePresignedUrl(new GeneratePresignedUrlRequest(bucketName, key).withExpiration(expiration)); }
From source file:org.mobicents.servlet.restcomm.amazonS3.S3AccessTool.java
License:Open Source License
public URI uploadFile(final String fileToUpload) { AWSCredentials credentials = new BasicAWSCredentials(accessKey, securityKey); AmazonS3 s3client = new AmazonS3Client(credentials); try {/*from w w w. j a v a 2s . co m*/ StringBuffer bucket = new StringBuffer(); bucket.append(bucketName); if (folder != null && !folder.isEmpty()) bucket.append("/").append(folder); URI fileUri = URI.create(fileToUpload); logger.info("File to upload to S3: " + fileUri.toString()); File file = new File(fileUri); // while (!file.exists()){} // logger.info("File exist: "+file.exists()); //First generate the Presigned URL, buy some time for the file to be written on the disk Date date = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(date); if (daysToRetainPublicUrl > 0) { cal.add(Calendar.DATE, daysToRetainPublicUrl); } else { //By default the Public URL will be valid for 180 days cal.add(Calendar.DATE, 180); } date = cal.getTime(); GeneratePresignedUrlRequest generatePresignedUrlRequestGET = new GeneratePresignedUrlRequest( bucket.toString(), file.getName()); generatePresignedUrlRequestGET.setMethod(HttpMethod.GET); generatePresignedUrlRequestGET.setExpiration(date); URL downloadUrl = s3client.generatePresignedUrl(generatePresignedUrlRequestGET); //Second upload the file to S3 // while (!file.exists()){} 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); } 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, which "); logger.error("Error Message: " + ace.getMessage()); return null; } catch (URISyntaxException e) { logger.error("URISyntaxException: " + e.getMessage()); return null; } }
From source file:org.restcomm.connect.commons.amazonS3.S3AccessTool.java
License:Open Source License
public URI getPublicUrl(String fileName) throws URISyntaxException { if (s3client == null) { s3client = getS3client();//w w w. j a va2s. c o m } Date date = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MINUTE, minutesToRetainPublicUrl); if (logger.isInfoEnabled()) { final String msg = String.format("Prepared amazon s3 public url valid for %s minutes for recording: %s", minutesToRetainPublicUrl, fileName); logger.info(msg); } date = cal.getTime(); String bucket = bucketName; if (folder != null && !folder.isEmpty()) { bucket = bucket.concat("/").concat(folder); } GeneratePresignedUrlRequest generatePresignedUrlRequestGET = new GeneratePresignedUrlRequest(bucket, fileName); generatePresignedUrlRequestGET.setMethod(HttpMethod.GET); generatePresignedUrlRequestGET.setExpiration(date); return s3client.generatePresignedUrl(generatePresignedUrlRequestGET).toURI(); }
From source file:S3Controller.DownloadingImages.java
public static void main(String[] args) throws IOException { AWSCredentials credentials = null;/* w w w.j a v a 2 s . c o m*/ String aws_access_key_id = "PUT_YOUR_aws_access_key_id_HERE"; String aws_secret_access_key = "PUT_YOUR_aws_secret_access_key_HERE"; try { credentials = new BasicAWSCredentials(aws_access_key_id, aws_secret_access_key);//.getCredentials(); } catch (Exception e) { throw new AmazonClientException("Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (~/.aws/credentials), and is in valid format.", e); } AmazonS3 s3 = new AmazonS3Client(credentials); Region AP_SOUTHEAST_1 = Region.getRegion(Regions.AP_SOUTHEAST_1); s3.setRegion(AP_SOUTHEAST_1); String bucketName = "PUT_YOUR_S3-BUCKET-NAME_HERE"; String key = "PUT_YOUR_S3-BUCKET-KEY_HERE"; try { ArrayList arr = new ArrayList(); ArrayList EmailArray = new ArrayList(); Bucket bucket = new Bucket(bucketName); ObjectListing objects = s3.listObjects(bucket.getName()); do { for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) { // System.out.println(objectSummary.getKey() + "\t" + // objectSummary.getSize() + "\t" + // StringUtils.fromDate(objectSummary.getLastModified())); arr.add(objectSummary.getKey()); } objects = s3.listNextBatchOfObjects(objects); } while (objects.isTruncated()); KrakenIOExampleMain kraken = new KrakenIOExampleMain(); for (int i = 0; i < arr.size(); i++) { System.out.println("Compressing: " + arr.get(i)); String s = (String) arr.get(i); GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket.getName(), s); System.out.println(s3.generatePresignedUrl(request)); URL Glink = s3.generatePresignedUrl(request); String Dlink = Glink.toString(); System.out.println("Download Link:" + Dlink); kraken.Compression(Dlink, bucketName); System.out.println("Compression completed: " + arr.get(i)); EmailArray.add("Processed Image:" + arr.get(i)); } System.out.println("Start Emailing list"); EmailSender esender = new EmailSender(); esender.EmailVerification(GetNotificationEmail, EmailArray); System.out.println("Kraken compression completed"); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon S3, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("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."); System.out.println("Error Message: " + ace.getMessage()); } catch (ExecutionException ex) { Logger.getLogger(DownloadingImages.class.getName()).log(Level.SEVERE, null, ex); } catch (InterruptedException ex) { Logger.getLogger(DownloadingImages.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:servlet.FileUploadServlet.java
private void putInBucket(String fileName, String id) throws IOException { AWSCredentials awsCreds = new PropertiesCredentials( new File("/Users/paulamontojo/Desktop/AwsCredentials.properties")); AmazonS3 s3client = new AmazonS3Client(awsCreds); try {/*from w ww . j a v a 2 s. c om*/ System.out.println("Uploading a new object to S3 from a file\n"); File file = new File(uploadFileName); s3client.putObject(new PutObjectRequest(bucketName, fileName, file)); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, fileName); generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default. URL s = s3client.generatePresignedUrl(generatePresignedUrlRequest); insertUrl(s.toString(), id); System.out.println(s); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("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."); System.out.println("Error Message: " + ace.getMessage()); } }