List of usage examples for com.amazonaws.services.s3.model GetObjectRequest GetObjectRequest
public GetObjectRequest(String bucketName, String key)
From source file:aws.sample.S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { /*/*from www. jav a 2 s . c o m*/ * Important: Be sure to fill in your AWS access credentials in the AwsCredentials.properties file before you try to run this sample. http://aws.amazon.com/security-credentials */ AmazonS3 s3 = new AmazonS3Client( new PropertiesCredentials(S3Sample.class.getResourceAsStream("/AwsCredentials.properties"))); String bucketName = "my-first-s3-bucket-" + UUID.randomUUID(); String key = "MyObjectKey"; System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); try { /* * Create a new S3 bucket - Amazon S3 bucket names are globally unique, so once a bucket name has been taken by any user, you can't create another bucket with that same name. * * You can optionally specify a location for your bucket if you want to keep your data closer to your applications or users. */ System.out.println("Creating bucket " + bucketName + "\n"); s3.createBucket(bucketName); /* * List the buckets in your account */ System.out.println("Listing buckets"); for (Bucket bucket : s3.listBuckets()) { System.out.println(" - " + bucket.getName()); } System.out.println(); /* * Upload an object to your bucket - You can easily upload a file to S3, or upload directly an InputStream if you know the length of the data in the stream. You can also specify your own metadata when uploading to S3, which allows you set a variety of options like content-type and content-encoding, plus additional metadata specific to your applications. */ System.out.println("Uploading a new object to S3 from a file\n"); s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile())); /* * Download an object - When you download an object, you get all of the object's metadata and a stream from which to read the contents. It's important to read the contents of the stream as quickly as possibly since the data is streamed directly from Amazon S3 and your network connection will remain open until you read all the data or close the input stream. * * GetObjectRequest also supports several other options, including conditional downloading of objects based on modification times, ETags, and selectively downloading a range of an object. */ System.out.println("Downloading an object"); S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); displayTextInputStream(object.getObjectContent()); /* * List objects in your bucket by prefix - There are many options for listing the objects in your bucket. Keep in mind that buckets with many objects might truncate their results when listing their objects, so be sure to check if the returned object listing is truncated, and use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve additional results. */ System.out.println("Listing objects"); ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My")); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println( " - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } System.out.println(); /* * Delete an object - Unless versioning has been turned on for your bucket, there is no way to undelete an object, so use caution when deleting objects. */ System.out.println("Deleting an object\n"); s3.deleteObject(bucketName, key); /* * Delete a bucket - A bucket must be completely empty before it can be deleted, so remember to delete any objects from your buckets before you try to delete them. */ System.out.println("Deleting bucket " + bucketName + "\n"); s3.deleteBucket(bucketName); } 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()); } }
From source file:br.com.tamandua.aws.S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { /*/*from ww w. j a v a2 s .co m*/ * Important: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this * sample. * http://aws.amazon.com/security-credentials */ AmazonS3 s3 = new AmazonS3Client( new PropertiesCredentials(S3Sample.class.getResourceAsStream("AwsCredentials.properties"))); String bucketName = "test-bucket-everton-" + UUID.randomUUID(); String key = "somekey"; System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); try { /* * Create a new S3 bucket - Amazon S3 bucket names are globally unique, * so once a bucket name has been taken by any user, you can't create * another bucket with that same name. * * You can optionally specify a location for your bucket if you want to * keep your data closer to your applications or users. */ System.out.println("Creating bucket " + bucketName + "\n"); s3.createBucket(bucketName); /* * List the buckets in your account */ System.out.println("Listing buckets"); for (Bucket bucket : s3.listBuckets()) { System.out.println(" - " + bucket.getName()); } System.out.println(); /* * Upload an object to your bucket - You can easily upload a file to * S3, or upload directly an InputStream if you know the length of * the data in the stream. You can also specify your own metadata * when uploading to S3, which allows you set a variety of options * like content-type and content-encoding, plus additional metadata * specific to your applications. */ System.out.println("Uploading a new object to S3 from a file\n"); s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile())); /* * Download an object - When you download an object, you get all of * the object's metadata and a stream from which to read the contents. * It's important to read the contents of the stream as quickly as * possibly since the data is streamed directly from Amazon S3 and your * network connection will remain open until you read all the data or * close the input stream. * * GetObjectRequest also supports several other options, including * conditional downloading of objects based on modification times, * ETags, and selectively downloading a range of an object. */ System.out.println("Downloading an object"); S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); displayTextInputStream(object.getObjectContent()); /* * List objects in your bucket by prefix - There are many options for * listing the objects in your bucket. Keep in mind that buckets with * many objects might truncate their results when listing their objects, * so be sure to check if the returned object listing is truncated, and * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve * additional results. */ System.out.println("Listing objects"); ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("some")); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println( " - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } System.out.println(); /* * Delete an object - Unless versioning has been turned on for your bucket, * there is no way to undelete an object, so use caution when deleting objects. */ System.out.println("Deleting an object\n"); s3.deleteObject(bucketName, key); /* * Delete a bucket - A bucket must be completely empty before it can be * deleted, so remember to delete any objects from your buckets before * you try to delete them. */ System.out.println("Deleting bucket " + bucketName + "\n"); s3.deleteBucket(bucketName); } 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()); } }
From source file:br.com.unb.aws.client.S3ClientV1.java
License:Open Source License
public static void main(String[] args) throws IOException { /*//from w w w. j a v a 2 s . c o m * Create your credentials file at ~/.aws/credentials (C:\Users\USER_NAME\.aws\credentials for Windows users) * and save the following lines after replacing the underlined values with your own. * * [default] * aws_access_key_id = YOUR_ACCESS_KEY_ID * aws_secret_access_key = YOUR_SECRET_ACCESS_KEY */ AmazonS3 s3 = new AmazonS3Client(); Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); String bucketName = "my-first-s3-bucket-" + UUID.randomUUID(); String key = "MyObjectKey"; System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); try { /* * Create a new S3 bucket - Amazon S3 bucket names are globally unique, * so once a bucket name has been taken by any user, you can't create * another bucket with that same name. * * You can optionally specify a location for your bucket if you want to * keep your data closer to your applications or users. */ System.out.println("Creating bucket " + bucketName + "\n"); s3.createBucket(bucketName); /* * List the buckets in your account */ System.out.println("Listing buckets"); for (Bucket bucket : s3.listBuckets()) { System.out.println(" - " + bucket.getName()); } System.out.println(); /* * Upload an object to your bucket - You can easily upload a file to * S3, or upload directly an InputStream if you know the length of * the data in the stream. You can also specify your own metadata * when uploading to S3, which allows you set a variety of options * like content-type and content-encoding, plus additional metadata * specific to your applications. */ System.out.println("Uploading a new object to S3 from a file\n"); s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile())); /* * Download an object - When you download an object, you get all of * the object's metadata and a stream from which to read the contents. * It's important to read the contents of the stream as quickly as * possibly since the data is streamed directly from Amazon S3 and your * network connection will remain open until you read all the data or * close the input stream. * * GetObjectRequest also supports several other options, including * conditional downloading of objects based on modification times, * ETags, and selectively downloading a range of an object. */ System.out.println("Downloading an object"); S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); displayTextInputStream(object.getObjectContent()); /* * List objects in your bucket by prefix - There are many options for * listing the objects in your bucket. Keep in mind that buckets with * many objects might truncate their results when listing their objects, * so be sure to check if the returned object listing is truncated, and * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve * additional results. */ System.out.println("Listing objects"); ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My")); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println( " - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } System.out.println(); /* * Delete an object - Unless versioning has been turned on for your bucket, * there is no way to undelete an object, so use caution when deleting objects. */ System.out.println("Deleting an object\n"); s3.deleteObject(bucketName, key); /* * Delete a bucket - A bucket must be completely empty before it can be * deleted, so remember to delete any objects from your buckets before * you try to delete them. */ System.out.println("Deleting bucket " + bucketName + "\n"); s3.deleteBucket(bucketName); } 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()); } }
From source file:br.unb.bionimbuz.storage.bucket.methods.CloudMethodsAmazonGoogle.java
@Override public void StorageDownloadFile(BioBucket bucket, String bucketPath, String localPath, String fileName) throws Exception { switch (bucket.getProvider()) { case AMAZON: { s3client.setEndpoint(bucket.getEndPoint()); S3Object object = s3client .getObject(new GetObjectRequest(bucket.getName(), bucketPath.substring(1) + fileName)); InputStream objectData = object.getObjectContent(); if (!(new File(localPath + fileName)).exists()) { Files.copy(objectData, Paths.get(localPath + fileName)); }/* w ww .j a v a2 s .co m*/ objectData.close(); break; } case GOOGLE: { String command = gcloudFolder + "gsutil cp gs://" + bucket.getName() + bucketPath + fileName + " " + localPath + fileName; ExecCommand(command); break; } default: { throw new Exception("Provedor incorreto!"); } } }
From source file:br.unb.cic.bionimbuz.services.storage.bucket.methods.CloudMethodsAmazonGoogle.java
@Override public void StorageDownloadFile(BioBucket bucket, String bucketPath, String localPath, String fileName) throws Exception { switch (bucket.getProvider()) { case AMAZON: { s3client.setEndpoint(bucket.getEndPoint()); final S3Object object = s3client .getObject(new GetObjectRequest(bucket.getName(), bucketPath.substring(1) + fileName)); final InputStream objectData = object.getObjectContent(); Files.copy(objectData, Paths.get(localPath + fileName)); objectData.close();/*from w w w . j a va 2 s. c om*/ break; } case GOOGLE: { final String command = gcloudFolder + "gsutil cp gs://" + bucket.getName() + bucketPath + fileName + " " + localPath + fileName; ExecCommand(command); break; } default: { throw new Exception("Provedor incorreto!"); } } }
From source file:br.unb.cic.bionimbuz.storage.bucket.methods.CloudMethodsAmazonGoogle.java
@Override public void StorageDownloadFile(BioBucket bucket, String bucketPath, String localPath, String fileName) throws Exception { switch (bucket.getProvider()) { case AMAZON: { s3client.setEndpoint(bucket.getEndPoint()); S3Object object = s3client .getObject(new GetObjectRequest(bucket.getName(), bucketPath.substring(1) + fileName)); InputStream objectData = object.getObjectContent(); Files.copy(objectData, Paths.get(localPath + fileName)); objectData.close();//from w ww . j a va2s . c o m break; } case GOOGLE: { String command = gcloudFolder + "gsutil cp gs://" + bucket.getName() + bucketPath + fileName + " " + localPath + fileName; ExecCommand(command); break; } default: { throw new Exception("Provedor incorreto!"); } } }
From source file:c3.ops.priam.backup.RangeReadInputStream.java
License:Apache License
public int read(final byte b[], final int off, final int len) throws IOException { // logger.info(String.format("incoming buf req's size = %d, off = %d, len to read = %d, on file size %d, cur offset = %d path = %s", // b.length, off, len, path.getSize(), offset, path.getRemotePath())); final long fileSize = path.getSize(); if (fileSize > 0 && offset >= fileSize) return -1; final long firstByte = offset; long curEndByte = firstByte + len; curEndByte = curEndByte <= fileSize ? curEndByte : fileSize; //need to subtract one as the call to getRange is inclusive //meaning if you want to download the first 10 bytes of a file, request bytes 0..9 final long endByte = curEndByte - 1; // logger.info(String.format("start byte = %d, end byte = %d", firstByte, endByte)); try {/*from w w w .j a v a 2 s . c om*/ Integer cnt = new RetryableCallable<Integer>() { public Integer retriableCall() throws IOException { GetObjectRequest req = new GetObjectRequest(bucketName, path.getRemotePath()); req.setRange(firstByte, endByte); S3ObjectInputStream is = null; try { is = s3Client.getObject(req).getObjectContent(); byte[] readBuf = new byte[4092]; int rCnt; int readTotal = 0; int incomingOffet = off; while ((rCnt = is.read(readBuf, 0, readBuf.length)) >= 0) { System.arraycopy(readBuf, 0, b, incomingOffet, rCnt); readTotal += rCnt; incomingOffet += rCnt; // logger.info(" local read cnt = " + rCnt + "Current Thread Name = "+Thread.currentThread().getName()); } if (readTotal == 0 && rCnt == -1) return -1; offset += readTotal; return Integer.valueOf(readTotal); } finally { IOUtils.closeQuietly(is); } } }.call(); // logger.info("read cnt = " + cnt); return cnt.intValue(); } catch (Exception e) { String msg = String.format("failed to read offset range %d-%d of file %s whose size is %d", firstByte, endByte, path.getRemotePath(), path.getSize()); throw new IOException(msg, e); } }
From source file:cloudtrailviewer.events.EventLoader.java
License:Open Source License
private void readS3File(String key) throws IOException { AWSCredentials credentials = new BasicAWSCredentials(PropertiesSingleton.getInstance().getProperty("Key"), PropertiesSingleton.getInstance().getProperty("Secret")); AmazonS3 s3Client = new AmazonS3Client(credentials); String bucketName = PropertiesSingleton.getInstance().getProperty("Bucket"); S3Object s3Object = s3Client.getObject(new GetObjectRequest(bucketName, key)); GZIPInputStream gzis = new GZIPInputStream(s3Object.getObjectContent()); BufferedReader bf = new BufferedReader(new InputStreamReader(gzis, "UTF-8")); String outStr = ""; String line;/*from ww w. java 2 s . c om*/ while ((line = bf.readLine()) != null) { outStr += line; } bf.close(); gzis.close(); readLogEvents(outStr); }
From source file:com.aegeus.aws.SimpleStorageService.java
License:Apache License
public S3Object getObject(String bucket, String key) { return s3.getObject(new GetObjectRequest(bucket, key)); }
From source file:com.amazon.sqs.javamessaging.AmazonSQSExtendedClient.java
License:Open Source License
private String getTextFromS3(String s3BucketName, String s3Key) { GetObjectRequest getObjectRequest = new GetObjectRequest(s3BucketName, s3Key); String embeddedText = null;// w ww.j av a2s. co m S3Object obj = null; try { obj = clientConfiguration.getAmazonS3Client().getObject(getObjectRequest); } catch (AmazonServiceException e) { String errorMessage = "Failed to get the S3 object which contains the message payload. Message was not received."; LOG.error(errorMessage, e); throw new AmazonServiceException(errorMessage, e); } catch (AmazonClientException e) { String errorMessage = "Failed to get the S3 object which contains the message payload. Message was not received."; LOG.error(errorMessage, e); throw new AmazonClientException(errorMessage, e); } try { InputStream objContent = obj.getObjectContent(); java.util.Scanner objContentScanner = new java.util.Scanner(objContent, "UTF-8"); objContentScanner.useDelimiter("\\A"); embeddedText = objContentScanner.hasNext() ? objContentScanner.next() : ""; objContentScanner.close(); objContent.close(); } catch (IOException e) { String errorMessage = "Failure when handling the message which was read from S3 object. Message was not received."; LOG.error(errorMessage, e); throw new AmazonClientException(errorMessage, e); } return embeddedText; }