List of usage examples for com.amazonaws.services.s3 AmazonS3 putObject
public PutObjectResult putObject(String bucketName, String key, String content) throws AmazonServiceException, SdkClientException;
Encodes a String into the contents of an S3 object.
From source file:aws.example.s3.PutObject.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 a file to\n" + "upload to it.\n" + "\n" + "Ex: PutObject <bucketname> <filename>\n"; if (args.length < 2) { System.out.println(USAGE); System.exit(1);//from www . j a va 2s . c o m } String bucket_name = args[0]; String file_path = args[1]; String key_name = Paths.get(file_path).getFileName().toString(); System.out.format("Uploading %s to S3 bucket %s...\n", file_path, bucket_name); final AmazonS3 s3 = new AmazonS3Client(); try { s3.putObject(bucket_name, key_name, file_path); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Done!"); }
From source file:com.atlantbh.jmeter.plugins.aws.s3.AWSS3Uploader.java
License:Apache License
@Override public SampleResult sample(Entry arg0) { LOGGER.info("Upload started...."); SampleResult result = new SampleResult(); result.setSampleLabel(getName());// w w w . j a v a 2 s . co m result.setDataType(SampleResult.TEXT); result.sampleStart(); try { BasicAWSCredentials creds = new BasicAWSCredentials(getKey(), getSecret()); AmazonS3 client = new AmazonS3Client(creds); client.putObject(getBucket(), getDestination(), new File(getObject())); LOGGER.info("Upload finished."); result.setResponseData("Upload finished".getBytes()); result.setSuccessful(!false); result.setResponseCode("200"); result.setResponseMessage("Uploaded"); } catch (Exception e) { LOGGER.info("Upload error."); result.setResponseData(("Upload error: " + e.getMessage()).getBytes()); result.setSuccessful(false); result.setResponseCode("500"); result.setResponseMessage("Error"); } result.sampleEnd(); return result; }
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 ww. j a v a 2 s.co m 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.casadocodigo.ecommerce.infra.AmazonFileSaver.java
public String write(Part part) { try {/*from w ww . ja va 2 s. c om*/ AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); String fileName = extractFilename(part.getHeader(CONTENT_DISPOSITION)); OutputStream out = null; InputStream filecontent = null; File f = new File(fileName); out = new FileOutputStream(f); filecontent = part.getInputStream(); int read; final byte[] bytes = new byte[1024]; while ((read = filecontent.read(bytes)) != -1) { out.write(bytes, 0, read); } s3client.putObject(BUCKET_NAME, fileName, f); return ""; } catch (FileNotFoundException ex) { Logger.getLogger(AmazonFileSaver.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException | AmazonClientException ex) { Logger.getLogger(AmazonFileSaver.class.getName()).log(Level.SEVERE, null, ex); } return ""; }
From source file:com.cloud.utils.S3Utils.java
License:Apache License
public static void putDirectory(final ClientOptions clientOptions, final String bucketName, final File directory, final FilenameFilter fileNameFilter, final ObjectNamingStrategy namingStrategy) { assert clientOptions != null; assert isNotBlank(bucketName); assert directory != null && directory.isDirectory(); assert fileNameFilter != null; assert namingStrategy != null; if (LOGGER.isDebugEnabled()) { LOGGER.debug(// ww w .j ava 2 s . c om format("Putting directory %1$s in S3 bucket %2$s.", directory.getAbsolutePath(), bucketName)); } // Determine the list of files to be sent using the passed filter ... final File[] files = directory.listFiles(fileNameFilter); if (LOGGER.isTraceEnabled()) { LOGGER.trace(format("Putting files (%1$s) in S3 bucket %2$s.", ArrayUtils.toString(files, "no files found"), bucketName)); } // Skip spinning up an S3 connection when no files will be sent ... if (isEmpty(files)) { return; } final AmazonS3 client = acquireClient(clientOptions); // Send the files to S3 using the passed ObjectNaming strategy to // determine the key ... for (final File file : files) { final String key = namingStrategy.determineKey(file); if (LOGGER.isDebugEnabled()) { LOGGER.debug(format("Putting file %1$s into bucket %2$s with key %3$s.", file.getAbsolutePath(), bucketName, key)); } client.putObject(bucketName, key, file); } }
From source file:com.easarrive.aws.plugins.common.service.impl.S3Service.java
License:Open Source License
/** * {@inheritDoc}// ww w .j av a2 s . co m */ @Override public PutObjectResult putObject(AmazonS3 client, String bucketName, String key, File file) { if (client == null) { return null; } else if (StringUtil.isEmpty(bucketName)) { return null; } else if (StringUtil.isEmpty(key)) { return null; } else if (file == null) { return null; } PutObjectResult result = null; if (!client.doesObjectExist(bucketName, key)) { result = client.putObject(bucketName, key, file); } return result; }
From source file:com.kirana.services.ProductServicesImpl.java
@Override public boolean uploadProductImage(File productCsv, Shop shop, String productCode) throws Exception { AmazonS3 s3client = getS3Client(); if (!s3client.doesBucketExist(S3_BUCKET_NAME)) { s3client.createBucket(S3_BUCKET_NAME); }//ww w .ja v a2 s .co m s3client.putObject(S3_BUCKET_NAME, shop.getName() + "/" + productCode, productCsv); return true; }
From source file:com.kodemore.aws.s3.KmS3Uploader.java
License:Open Source License
/** * Upload a file from the local file system to the remote s3 repository. * The toPath (at s3) should NOT begin with a slash (/). *//*from ww w. jav a 2 s .co m*/ public void upload(String bucketName, String toPath, File fromFile) { AmazonS3 s3; s3 = createClient(); s3.putObject(bucketName, toPath, fromFile); }
From source file:datameer.awstasks.util.S3Util.java
License:Apache License
public static void uploadFile(AmazonS3 s3Service, String bucket, File file, String remotePath) { if (remotePath.startsWith("/")) { remotePath = remotePath.substring(1); }/*from w w w. j a v a2 s . co m*/ s3Service.putObject(bucket, remotePath, file); }
From source file:org.cloudml.connectors.BeanstalkConnector.java
License:Open Source License
public void prepareWar(File warFile, String versionLabel, String applicationName) { AmazonS3 s3 = new AmazonS3Client(awsCredentials); String bucketName = beanstalkClient.createStorageLocation().getS3Bucket(); String key;//from w w w .j av a 2 s .co m try { key = URLEncoder.encode(warFile.getName() + "-" + versionLabel, "UTF-8"); s3.putObject(bucketName, key, warFile); beanstalkClient.createApplicationVersion(new CreateApplicationVersionRequest() .withApplicationName(applicationName).withAutoCreateApplication(true) .withVersionLabel(versionLabel).withSourceBundle(new S3Location(bucketName, key))); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block journal.log(Level.SEVERE, e.getMessage()); } }