List of usage examples for com.amazonaws.services.s3.model GetObjectRequest GetObjectRequest
public GetObjectRequest(String bucketName, String key)
From source file:com.arc.cloud.aws.s3.S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { /*/*from ww w . ja v a2s . c o m*/ * The ProfileCredentialsProvider will return your [default] * credential profile by reading from the credentials file located at * (~/.aws/credentials). */ AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider().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 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:com.athena.dolly.web.aws.s3.S3Service.java
License:Open Source License
/** * Getting file from s3 using key. File will be saved in temporary directory you configured * @param bucketName//from w w w. j a va 2 s . c o m * @param key */ public File getObject(String bucketName, String key) { String tempFileName = tempDir + File.separator + key.substring(key.lastIndexOf("/") + 1); File tempFile = new File(tempFileName); ObjectMetadata object = s3.getObject(new GetObjectRequest(bucketName, key), tempFile); return tempFile; }
From source file:com.atlassian.localstack.sample.S3Sample.java
License:Open Source License
public static void runTest(AWSCredentials credentials) throws IOException { AmazonS3 s3 = new AmazonS3Client(credentials); Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2);//from ww w. jav a 2s. c om s3.setEndpoint(LocalstackTestRunner.getEndpointS3()); 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"); /* * 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); }
From source file:com.att.aro.core.cloud.aws.AwsRepository.java
License:Apache License
@Override public String get(String remotePath, String localPath) { String downloadedFilePath = ""; if (s3Client != null && (localPath != null && localPath.length() > 0) && (remotePath != null && remotePath.length() > 0)) { downloadedFilePath = localPath + "/" + remotePath; try {/*ww w. jav a 2 s . c om*/ File downloadTarget = new File(downloadedFilePath); Download myDownload = transferMgr.download(new GetObjectRequest(bucketName, remotePath), downloadTarget); myDownload.waitForCompletion(); transferMgr.shutdownNow(); } catch (AmazonClientException ace) { LOGGER.error("Error Message: " + ace.getMessage()); } catch (InterruptedException ire) { LOGGER.error(ire.getMessage()); } catch (Exception exception) { LOGGER.error(exception.getMessage(), exception); } } return downloadedFilePath; }
From source file:com.BoomPi.ImageResizeHandlerFromS3.java
License:Apache License
private RawImage getOriginalImage(S3Event s3Event) throws Exception { S3EventNotificationRecord record = s3Event.getRecords().get(0); String srcBucket = record.getS3().getBucket().getName(); String srcKey = record.getS3().getObject().getKey().replace('+', ' '); srcKey = URLDecoder.decode(srcKey, "UTF-8"); if (!new FormatValidator(srcKey).isValid()) throw new Exception("not valid file format"); BufferedImage srcImage = null; AmazonS3 s3Client = new AmazonS3Client(); try (S3Object s3Object = s3Client.getObject(new GetObjectRequest(srcBucket, srcKey));) { InputStream objectData = s3Object.getObjectContent(); srcImage = ImageIO.read(objectData); }// w w w. j a v a2 s . com // TODO implement other type of RawImage model JpegRawImage jpegRawImage = new JpegRawImage(srcImage.getWidth(), srcImage.getHeight(), srcBucket, srcKey, srcImage); return jpegRawImage; }
From source file:com.carrotgarden.nexus.aws.s3.publish.amazon.AmazonServiceProvider.java
License:BSD License
@Override public boolean load(final String path, final File file) { reporter.requestLoadCount.inc();//from ww w . j av a 2 s .c o m reporter.requestTotalCount.inc(); try { final GetObjectRequest request = // new GetObjectRequest(mavenBucket(), mavenRepoKey(path)); final ObjectMetadata result = client.getObject(request, file); reporter.fileLoadCount.inc(); reporter.fileLoadSize.inc(file.length()); reporter.fileLoadWatch.add(file); setAvailable(true, null); return true; } catch (final AmazonS3Exception e) { switch (e.getStatusCode()) { case 404: log.error("path={} code={}", path, e.getErrorCode()); break; default: setAvailable(true, e); break; } return false; } catch (final Exception e) { setAvailable(false, e); return false; } }
From source file:com.clicktravel.infrastructure.persistence.aws.s3.S3FileStore.java
License:Apache License
@Override public FileItem read(final FilePath filePath) throws NonExistentItemException { checkInitialization();/*from w w w . j ava 2s. c om*/ final GetObjectRequest getObjectRequest = new GetObjectRequest(bucketNameForFilePath(filePath), filePath.filename()); try { final S3Object s3Object = amazonS3Client.getObject(getObjectRequest); final Map<String, String> userMetaData = getUserMetaData(s3Object); final String filename = userMetaData.get(USER_METADATA_FILENAME); final String lastUpdatedTimeStr = userMetaData.get(USER_METADATA_LAST_UPDATED_TIME); DateTime lastUpdatedTime = null; if (lastUpdatedTimeStr != null) { try { lastUpdatedTime = formatter.parseDateTime(lastUpdatedTimeStr); } catch (final Exception e) { logger.warn(e.getMessage(), e); } } try { final FileItem fileItem = new FileItem(filename, s3Object.getObjectContent(), lastUpdatedTime); return fileItem; } catch (final IOException e) { throw new IllegalStateException(e); } } catch (final AmazonS3Exception e) { if (missingItemErrorCodes.contains(e.getErrorCode())) { throw new NonExistentItemException( "Item does not exist" + filePath.directory() + "->" + filePath.filename()); } throw e; } }
From source file:com.cloud.utils.S3Utils.java
License:Apache License
@SuppressWarnings("unchecked") public static File getFile(final ClientOptions clientOptions, final String bucketName, final String key, final File targetDirectory, final FileNamingStrategy namingStrategy) { assert clientOptions != null; assert isNotBlank(bucketName); assert isNotBlank(key); assert targetDirectory != null && targetDirectory.isDirectory(); assert namingStrategy != null; final AmazonS3 connection = acquireClient(clientOptions); File tempFile = null;/*from w w w . j ava 2 s . co m*/ try { tempFile = createTempFile(join(asList(targetDirectory.getName(), currentTimeMillis(), "part"), "-"), "tmp", targetDirectory); tempFile.deleteOnExit(); if (LOGGER.isDebugEnabled()) { LOGGER.debug(format("Downloading object %1$s from bucket %2$s to temp file %3$s", key, bucketName, tempFile.getName())); } connection.getObject(new GetObjectRequest(bucketName, key), tempFile); final File targetFile = new File(targetDirectory, namingStrategy.determineFileName(key)); tempFile.renameTo(targetFile); return targetFile; } catch (FileNotFoundException e) { throw new CloudRuntimeException( format("Failed open file %1$s in order to get object %2$s from bucket %3$s.", targetDirectory.getAbsoluteFile(), bucketName, key), e); } catch (IOException e) { throw new CloudRuntimeException( format("Unable to allocate temporary file in directory %1$s to download %2$s:%3$s from S3", targetDirectory.getAbsolutePath(), bucketName, key), e); } finally { if (tempFile != null) { tempFile.delete(); } } }
From source file:com.clouddrive.parth.AmazonOperations.java
public byte[] downloadFile(String fileName, String userName) { S3Object object = s3.getObject(new GetObjectRequest(userName, fileName)); byte[] fileBytes = null; try {//w ww .j a v a 2 s. c om IOUtils.copy(object.getObjectContent(), new FileOutputStream(userName + "" + fileName)); File file = new File(userName + "" + fileName); FileInputStream fis = new FileInputStream(file); BufferedInputStream inputStream = new BufferedInputStream(fis); fileBytes = new byte[(int) file.length()]; inputStream.read(fileBytes); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return fileBytes; }
From source file:com.crickdata.upload.s3.UploadLiveData.java
License:Open Source License
public Map<String, Date> uploadToS3(String fileName, boolean type) throws IOException { Statistics statistics = new Statistics(); Map<String, Date> perfMap = new HashMap<String, Date>(); AWSCredentials credentials = null;// w w w. j a v a 2 s. c o m try { credentials = new BasicAWSCredentials("AKIAI6QKTRAQE7MXQOIQ", "wIG6u1yI5ZaseeJbvYSUmD98qelIJNSCVBzt5k2q"); } 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 (C:\\Users\\bssan_000\\.aws\\credentials), and is in valid format.", e); } AmazonS3 s3 = new AmazonS3Client(credentials); Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); String bucketName; if (!type) bucketName = "cricmatchinfo"; else bucketName = "cricmatchinfoseries"; String key = fileName.replace(".json", "").trim(); try { perfMap.put("S3INSERTREQ", new Date()); statistics.setS3Req(new Date()); File f = readMatchFile(fileName); double bytes = f.length(); double kilobytes = (bytes / 1024); System.out.println("Details :" + kilobytes); s3.putObject(new PutObjectRequest(bucketName, key, f)); statistics.setSize(String.valueOf(kilobytes)); S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); perfMap.put("S3SAVERES", object.getObjectMetadata().getLastModified()); statistics.setKey(key); statistics.setS3Res(object.getObjectMetadata().getLastModified()); MyUI.stats.add(statistics); displayTextInputStream(object.getObjectContent()); ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My")); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println( " - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } } 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()); } return perfMap; }