List of usage examples for com.amazonaws.services.s3 AmazonS3 getObject
ObjectMetadata getObject(GetObjectRequest getObjectRequest, File destinationFile)
throws SdkClientException, AmazonServiceException;
Gets the object metadata for the object stored in Amazon S3 under the specified bucket and key, and saves the object contents to the specified file.
From source file:aws.example.s3.GetObject.java
License:Open Source License
public static void main(String[] args) { final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket and object to\n" + "download from it.\n" + "\n" + "Ex: GetObject <bucketname> <filename>\n"; if (args.length < 2) { System.out.println(USAGE); System.exit(1);//from w w w . j ava 2 s. c om } String bucket_name = args[0]; String key_name = args[1]; System.out.format("Downloading %s from S3 bucket %s...\n", key_name, bucket_name); final AmazonS3 s3 = new AmazonS3Client(); try { S3Object o = s3.getObject(bucket_name, key_name); S3ObjectInputStream s3is = o.getObjectContent(); FileOutputStream fos = new FileOutputStream(new File(key_name)); byte[] read_buf = new byte[1024]; int read_len = 0; while ((read_len = s3is.read(read_buf)) > 0) { fos.write(read_buf, 0, read_len); } s3is.close(); fos.close(); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } catch (FileNotFoundException e) { System.err.println(e.getMessage()); System.exit(1); } catch (IOException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Done!"); }
From source file:ch.hesso.master.sweetcity.utils.PictureUtils.java
License:Apache License
public static InputStream getPicture(Key key) { if (key == null) return null; try {//from w w w .j av a 2 s. c om ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setProtocol(Protocol.HTTP); AmazonS3 s3Connection = new AmazonS3Client(AWS_CREDENTIALS, clientConfig); s3Connection.setEndpoint(ConstantsAWS.S3_END_POINT); S3Object obj = s3Connection.getObject(ConstantsAWS.S3_BUCKET_NAME, key.toString()); return obj.getObjectContent(); } catch (Exception e) { Log.d(Constants.PROJECT_NAME, e.toString()); } return null; }
From source file:com.amediamanager.config.S3ConfigurationProvider.java
License:Apache License
@Override public void loadProperties() { this.properties = null; // Load properties if there is a bucket and key if (bucket != null && key != null) { AWSCredentialsProvider creds = new AWSCredentialsProviderChain(new InstanceProfileCredentialsProvider(), new EnvironmentVariableCredentialsProvider(), new SystemPropertiesCredentialsProvider()); AmazonS3 s3Client = new AmazonS3Client(creds); try {/*from ww w . ja v a 2 s . co m*/ S3Object object = s3Client.getObject(this.bucket, this.key); if (object != null) { this.properties = new Properties(); try { this.properties.load(object.getObjectContent()); } catch (IOException e) { this.properties = null; LOG.warn("Found configuration file in S3 but failed to load properties (s3://{}/{})", new Object[] { this.bucket, this.key, e }); } finally { try { object.close(); } catch (IOException e) { // Don't care } } } } catch (AmazonS3Exception ase) { LOG.error("Error loading config from s3://{}/{}", new Object[] { this.bucket, this.key, ase }); } } }
From source file:com.bigstep.S3Sampler.java
License:Apache License
@Override public SampleResult runTest(JavaSamplerContext context) { // pull parameters String bucket = context.getParameter("bucket"); String object = context.getParameter("object"); String method = context.getParameter("method"); String local_file_path = context.getParameter("local_file_path"); String key_id = context.getParameter("key_id"); String secret_key = context.getParameter("secret_key"); String proxy_host = context.getParameter("proxy_host"); String proxy_port = context.getParameter("proxy_port"); String endpoint = context.getParameter("endpoint"); log.debug("runTest:method=" + method + " local_file_path=" + local_file_path + " bucket=" + bucket + " object=" + object); SampleResult result = new SampleResult(); result.sampleStart(); // start stopwatch try {//from w w w . j av a 2 s .c om ClientConfiguration config = new ClientConfiguration(); if (proxy_host != null && !proxy_host.isEmpty()) { config.setProxyHost(proxy_host); } if (proxy_port != null && !proxy_port.isEmpty()) { config.setProxyPort(Integer.parseInt(proxy_port)); } //config.setProtocol(Protocol.HTTP); AWSCredentials credentials = new BasicAWSCredentials(key_id, secret_key); AmazonS3 s3Client = new AmazonS3Client(credentials, config); if (endpoint != null && !endpoint.isEmpty()) { s3Client.setEndpoint(endpoint); } ObjectMetadata meta = null; if (method.equals("GET")) { File file = new File(local_file_path); //meta= s3Client.getObject(new GetObjectRequest(bucket, object), file); S3Object s3object = s3Client.getObject(bucket, object); S3ObjectInputStream stream = s3object.getObjectContent(); //while(stream.skip(1024*1024)>0); stream.close(); } else if (method.equals("PUT")) { File file = new File(local_file_path); s3Client.putObject(bucket, object, file); } result.sampleEnd(); // stop stopwatch result.setSuccessful(true); if (meta != null) { result.setResponseMessage( "OK on url:" + bucket + "/" + object + ". Length=" + meta.getContentLength()); } else { result.setResponseMessage("OK on url:" + bucket + "/" + object + ".No metadata"); } result.setResponseCodeOK(); // 200 code } catch (Exception e) { result.sampleEnd(); // stop stopwatch result.setSuccessful(false); result.setResponseMessage("Exception: " + e); // get stack trace as a String to return as document data java.io.StringWriter stringWriter = new java.io.StringWriter(); e.printStackTrace(new java.io.PrintWriter(stringWriter)); result.setResponseData(stringWriter.toString()); result.setDataType(org.apache.jmeter.samplers.SampleResult.TEXT); result.setResponseCode("500"); } return result; }
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 a va 2 s. c o 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.dateofrock.simpledbmapper.Reflector.java
License:Apache License
@SuppressWarnings("unchecked") <T> void setAttributeAndBlobValueToField(AmazonS3 s3, T instance, Field field, String attributeName, String attributeValue) throws IllegalAccessException, ParseException { Class<?> type;/*from w w w . j a va 2s . c o m*/ type = field.getType(); // SimpleDBAttribute SimpleDBAttribute sdbAttrAnnotation = field.getAnnotation(SimpleDBAttribute.class); if (sdbAttrAnnotation != null && getAttributeName(field).equals(attributeName)) { if (Set.class.isAssignableFrom(type)) { // Set Set<?> s = (Set<?>) field.get(instance); ParameterizedType genericType = (ParameterizedType) field.getGenericType(); Class<?> setClass = (Class<?>) genericType.getActualTypeArguments()[0]; if (Number.class.isAssignableFrom(setClass)) { // Set???Number?? if (s == null) { Set<Number> newSet = new HashSet<Number>(); field.set(instance, newSet); s = (Set<Number>) field.get(instance); } Number n = null; if (isIntegerType(setClass)) { n = new Integer(attributeValue); } else if (isFloatType(setClass)) { n = new Float(attributeValue); } else if (isLongType(setClass)) { n = new Long(attributeValue); } ((Set<Number>) s).add(n); return; } else if (isStringType(setClass)) { // Set???String?? if (s == null) { Set<String> newSet = new HashSet<String>(); field.set(instance, newSet); s = (Set<String>) field.get(instance); } ((Set<String>) s).add(attributeValue); return; } else { // FIXME throw new SimpleDBMapperUnsupportedTypeException( s.toString() + " genericType: " + setClass + " is not supported."); } } else if (isDateType(type)) { Date parsedDate = decodeDate(attributeValue); field.set(instance, parsedDate); return; } else if (isStringType(type)) { field.set(instance, attributeValue); return; } else if (isIntegerType(type)) { field.set(instance, new Integer(attributeValue)); return; } else if (isFloatType(type)) { field.set(instance, new Float(attributeValue)); return; } else if (isLongType(type)) { field.set(instance, new Long(attributeValue)); return; } else if (isBooleanType(type)) { field.set(instance, new Boolean(attributeValue)); } else { if (type.isAssignableFrom(List.class)) { new SimpleDBMapperUnsupportedTypeException(type + " is not supprted. Use java.util.Set."); } throw new SimpleDBMapperUnsupportedTypeException(type + " is not supprted."); } } // SimpleDBBlob SimpleDBBlob sdbBlobAnnotation = field.getAnnotation(SimpleDBBlob.class); if (sdbBlobAnnotation != null && getAttributeName(field).equals(attributeName)) { S3TaskResult taskResult = new S3TaskResult(Operation.DOWNLOAD, attributeName, null, null); taskResult.setSimpleDBAttributeValue(attributeValue); S3Object s3Obj = s3.getObject(taskResult.getBucketName(), taskResult.getKey()); InputStream input = s3Obj.getObjectContent(); if (isStringType(type)) { // FIXME encoding??? String stringValue = IOUtils.readString(input, "UTF-8"); field.set(instance, stringValue); } else if (isPrimitiveByteArrayType(type)) { byte[] bytes = IOUtils.readBytes(input); field.set(instance, bytes); } } }
From source file:com.easarrive.aws.plugins.common.service.impl.S3Service.java
License:Open Source License
/** * {@inheritDoc}// w w w . ja v a 2 s . c o m */ @Override public S3Object getObject(AmazonS3 client, String bucketName, String key) { if (client == null) { return null; } else if (StringUtil.isEmpty(bucketName)) { return null; } else if (StringUtil.isEmpty(key)) { return null; } S3Object object = null; if (client.doesObjectExist(bucketName, key)) { object = client.getObject(bucketName, key); } return object; }
From source file:com.flipzu.PostProcThread.java
License:Apache License
private void consolidateS3(Broadcast bcast) { debug.logPostProc("PostProcThread, consolidate S3 for " + bcast); File file = new File(bcast.getFilename()); if (!file.exists()) { debug.logPostProc("consolidateS3, empty broadcast, doing nothing"); return;/*from ww w .jav a 2 s .com*/ } AmazonS3 s3 = null; try { InputStream is = new FileInputStream("aws.properties"); s3 = new AmazonS3Client(new PropertiesCredentials(is)); } catch (Exception e) { debug.logError("consolidateS3 Error ", e); return; } String bucketName = Config.getInstance().getS3Bucket(); String dirName = Config.getInstance().getS3dir(); String objName = dirName + "/" + bcast.getId() + Config.getInstance().getFileWriterExtension(); S3Object obj = null; try { obj = s3.getObject(bucketName, objName); } catch (AmazonServiceException ase) { debug.logPostProc("consolidateS3 for " + bcast + ". File not found, doing nothing..."); return; } catch (AmazonClientException ace) { debug.logPostProc("consolidateS3 for " + bcast + ". File not found, doing nothing..."); return; } if (obj == null) { debug.logPostProc("consolidateS3 for " + bcast + ". File not found, doing nothing."); return; } debug.logPostProc("consolidateS3 for " + bcast + ". File found, consolidating."); String auxFile = Config.getInstance().getFileWriterDestDir() + "/" + bcast.getId() + "-aux" + Config.getInstance().getFileWriterExtension(); BufferedOutputStream bosAux = null; try { FileOutputStream fos = new FileOutputStream(auxFile); bosAux = new BufferedOutputStream(fos); } catch (FileNotFoundException e) { debug.logError("consolidateS3 for, error creating output stream", e); return; } BufferedInputStream is = new BufferedInputStream(obj.getObjectContent()); /* fetch file from S3 */ int r = 0; do { byte[] b = new byte[1024]; try { r = is.read(b); if (r > 0) bosAux.write(b, 0, r); } catch (IOException e) { debug.logError("consolidateS3 error", e); /* cleanup */ File aFile = new File(auxFile); aFile.delete(); return; } } while (r > 0); try { is.close(); } catch (IOException e) { debug.logError("consolidateS3 error", e); } /* append our file to aux file */ BufferedInputStream bis; try { FileInputStream fis = new FileInputStream(bcast.getFilename()); bis = new BufferedInputStream(fis); } catch (FileNotFoundException e) { debug.logPostProc("consolidateS3 error, FileNotFoundException"); return; } r = 0; do { byte[] b = new byte[1024]; try { r = bis.read(b); bosAux.write(b); } catch (IOException e) { debug.logError("consolidateS3 error", e); return; } } while (r > 0); try { bis.close(); bosAux.close(); } catch (IOException e) { debug.logError("consolidateS3 error", e); } /* delete old crap */ file.delete(); bcast.setFilename(auxFile); debug.logPostProc("consolidateS3 for " + bcast + ". File consolidated in " + bcast.getFilename()); return; }
From source file:com.images3.data.impl.ImageContentAccessImplS3.java
License:Apache License
@Override public File selectImageContent(ImageIdentity id, AmazonS3Bucket bucket) { File imageContent = new File(generateFilePath(id)); if (imageContent.exists()) { return imageContent; }/* ww w . ja v a2s .com*/ AmazonS3 client = clients.getClient(bucket); try { client.getObject(new GetObjectRequest(bucket.getName(), generateS3ObjectKey(id)), imageContent); } catch (AmazonS3Exception e) { if (e.getStatusCode() == 404) { throw new NoSuchEntityFoundException("ImageContent", generateS3ObjectKey(id), "No such image content found."); } throw new RuntimeException(e); } return imageContent; }
From source file:com.netflix.spinnaker.clouddriver.aws.provider.view.AmazonS3DataProvider.java
License:Apache License
protected S3Object fetchObject(String bucketAccount, String bucketRegion, String bucketName, String objectId) { NetflixAmazonCredentials account = (NetflixAmazonCredentials) accountCredentialsRepository .getOne(bucketAccount);/*from w w w . j a v a2s . c om*/ AmazonS3 amazonS3 = amazonClientProvider.getAmazonS3(account, bucketRegion); return amazonS3.getObject(bucketName, objectId); }