List of usage examples for com.amazonaws.services.s3.model ObjectMetadata ObjectMetadata
public ObjectMetadata()
From source file:br.puc_rio.ele.lvc.interimage.common.udf.ROIStorage.java
License:Apache License
/** * Method invoked on every tuple during foreach evaluation. * @param input tuple<br>/*from www . j a v a 2s . c om*/ * first column is assumed to have the geometry<br> * second column is assumed to have the class name<br> * third column is assumed to have the output path * @exception java.io.IOException * @return true if successful, false otherwise */ @Override public Boolean exec(Tuple input) throws IOException { if (input == null || input.size() < 3) return null; try { Object objGeometry = input.get(0); Geometry geometry = _geometryParser.parseGeometry(objGeometry); String className = DataType.toString(input.get(1)); String path = DataType.toString(input.get(2)); AWSCredentials credentials = new BasicAWSCredentials(_accessKey, _secretKey); AmazonS3 conn = new AmazonS3Client(credentials); conn.setEndpoint("https://s3.amazonaws.com"); /*File temp = File.createTempFile(className, ".wkt"); // Delete temp file when program exits. temp.deleteOnExit(); BufferedWriter out = new BufferedWriter(new FileWriter(temp)); out.write(new WKTWriter().write(geometry)); out.close();*/ /* File temp = File.createTempFile(className, ".wkt.snappy"); temp.deleteOnExit();*/ String geom = new WKTWriter().write(geometry); ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream snappyOut = new SnappyOutputStream(out); snappyOut.write(geom.getBytes()); snappyOut.close(); /*PutObjectRequest putObjectRequest = new PutObjectRequest(_bucket, path + className + ".wkt.snappy", temp); putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); // public for all*/ PutObjectRequest putObjectRequest = new PutObjectRequest(_bucket, path + className + ".wkts", new ByteArrayInputStream(out.toByteArray()), new ObjectMetadata()); putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); // public for all TransferManager tx = new TransferManager(credentials); tx.upload(putObjectRequest); return true; } catch (Exception e) { throw new IOException("Caught exception processing input row ", e); } }
From source file:ch.admin.isb.hermes5.persistence.s3.S3RemoteAdapter.java
License:Apache License
@Override @Asynchronous/*www .j a v a 2 s . c o m*/ @Logged public Future<Void> addFile(InputStream file, long size, String path) { try { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(size); metadata.setContentType(mimeTypeUtil.getMimeType(path)); s3.putObject(new PutObjectRequest(bucketName.getStringValue(), path, file, metadata) .withCannedAcl(CannedAccessControlList.PublicRead)); return new AsyncResult<Void>(null); } catch (Exception e) { throw new RuntimeException(e); } finally { if (file != null) { try { file.close(); } catch (IOException e) { } } } }
From source file:ch.admin.isb.hermes5.tools.filebackup.FileBackup.java
License:Apache License
public void run(String bucketName, String accessKey, String secretKey, String source, String targetPrefix, Long retentionPeriod, String topicArn, String snsEndpoint, String s3Endpoint) { AmazonS3 s3 = s3(accessKey, secretKey, s3Endpoint); AmazonSNS sns = sns(accessKey, secretKey, snsEndpoint); List<String> errors = new ArrayList<String>(); String[] list = new File(source).list(); for (String string : list) { File file = new File(source + "/" + string); System.out.print(timestamp() + " Backing up " + file.getAbsolutePath() + " to " + bucketName + "/" + targetPrefix + string + "..."); try {/*from ww w . j a v a2s. c o m*/ byte[] data = readFileToByteArray(file); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(data.length); s3.putObject(bucketName, targetPrefix + string, new ByteArrayInputStream(data), metadata); System.out.println("done"); long lastModified = file.lastModified(); long now = System.currentTimeMillis(); if (retentionPeriod > 0 && differenceInDays(lastModified, now) > retentionPeriod) { System.out.println(timestamp() + " File " + source + "/" + string + " is removed because it is older than " + retentionPeriod + " days."); boolean delete = file.delete(); if (!delete) { errors.add("Unable to delete " + file.getAbsolutePath()); } } } catch (Exception e) { System.out.println("failed " + e.getMessage()); errors.add(timestamp() + " Problem Backing up " + file.getAbsolutePath() + " to " + bucketName + "/" + targetPrefix + string + "\n" + getStackTrace(e)); } } if (errors.size() > 0) { StringBuilder sb = new StringBuilder(); for (String string : errors) { sb.append(string).append("\n"); } try { sendMessageThroughSNS(topicArn, sns, sb.toString(), "Problem with backup"); } catch (Exception e) { System.out.println(timestamp() + "ERROR: unable to report issue " + sb.toString()); e.printStackTrace(); } } }
From source file:ch.entwine.weblounge.maven.S3DeployMojo.java
License:Open Source License
/** * /*from w w w .j av a 2s.co m*/ * {@inheritDoc} * * @see org.apache.maven.plugin.Mojo#execute() */ public void execute() throws MojoExecutionException, MojoFailureException { // Setup AWS S3 client AWSCredentials credentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey); AmazonS3Client uploadClient = new AmazonS3Client(credentials); TransferManager transfers = new TransferManager(credentials); // Make sure key prefix does not start with a slash but has one at the // end if (keyPrefix.startsWith("/")) keyPrefix = keyPrefix.substring(1); if (!keyPrefix.endsWith("/")) keyPrefix = keyPrefix + "/"; // Keep track of how much data has been transferred long totalBytesTransferred = 0L; int items = 0; Queue<Upload> uploads = new LinkedBlockingQueue<Upload>(); try { // Check if S3 bucket exists getLog().debug("Checking whether bucket " + bucket + " exists"); if (!uploadClient.doesBucketExist(bucket)) { getLog().error("Desired bucket '" + bucket + "' does not exist!"); return; } getLog().debug("Collecting files to transfer from " + resources.getDirectory()); List<File> res = getResources(); for (File file : res) { // Make path of resource relative to resources directory String filename = file.getName(); String extension = FilenameUtils.getExtension(filename); String path = file.getPath().substring(resources.getDirectory().length()); String key = concat("/", keyPrefix, path).substring(1); // Delete old file version in bucket getLog().debug("Removing existing object at " + key); uploadClient.deleteObject(bucket, key); // Setup meta data ObjectMetadata meta = new ObjectMetadata(); meta.setCacheControl("public, max-age=" + String.valueOf(valid * 3600)); FileInputStream fis = null; GZIPOutputStream gzipos = null; final File fileToUpload; if (gzip && ("js".equals(extension) || "css".equals(extension))) { try { fis = new FileInputStream(file); File gzFile = File.createTempFile(file.getName(), null); gzipos = new GZIPOutputStream(new FileOutputStream(gzFile)); IOUtils.copy(fis, gzipos); fileToUpload = gzFile; meta.setContentEncoding("gzip"); if ("js".equals(extension)) meta.setContentType("text/javascript"); if ("css".equals(extension)) meta.setContentType("text/css"); } catch (FileNotFoundException e) { getLog().error(e); continue; } catch (IOException e) { getLog().error(e); continue; } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(gzipos); } } else { fileToUpload = file; } // Do a random check for existing errors before starting the next upload if (erroneousUpload != null) break; // Create put object request long bytesToTransfer = fileToUpload.length(); totalBytesTransferred += bytesToTransfer; PutObjectRequest request = new PutObjectRequest(bucket, key, fileToUpload); request.setProgressListener(new UploadListener(credentials, bucket, key, bytesToTransfer)); request.setMetadata(meta); // Schedule put object request getLog().info( "Uploading " + key + " (" + FileUtils.byteCountToDisplaySize((int) bytesToTransfer) + ")"); Upload upload = transfers.upload(request); uploads.add(upload); items++; } } catch (AmazonServiceException e) { getLog().error("Uploading resources failed: " + e.getMessage()); } catch (AmazonClientException e) { getLog().error("Uploading resources failed: " + e.getMessage()); } // Wait for uploads to be finished String currentUpload = null; try { Thread.sleep(1000); getLog().info("Waiting for " + uploads.size() + " uploads to finish..."); while (!uploads.isEmpty()) { Upload upload = uploads.poll(); currentUpload = upload.getDescription().substring("Uploading to ".length()); if (TransferState.InProgress.equals(upload.getState())) getLog().debug("Waiting for upload " + currentUpload + " to finish"); upload.waitForUploadResult(); } } catch (AmazonServiceException e) { throw new MojoExecutionException("Error while uploading " + currentUpload); } catch (AmazonClientException e) { throw new MojoExecutionException("Error while uploading " + currentUpload); } catch (InterruptedException e) { getLog().debug("Interrupted while waiting for upload to finish"); } // Check for errors that happened outside of the actual uploading if (erroneousUpload != null) { throw new MojoExecutionException("Error while uploading " + erroneousUpload); } getLog().info("Deployed " + items + " files (" + FileUtils.byteCountToDisplaySize((int) totalBytesTransferred) + ") to s3://" + bucket); }
From source file:ch.hesso.master.sweetcity.utils.PictureUtils.java
License:Apache License
public static Key uploadPicture(Bitmap picture, GoogleAccountCredential googleCredential) { if (picture == null) return null; try {/*www . java2 s . co m*/ ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setProtocol(Protocol.HTTP); AmazonS3 s3Connection = new AmazonS3Client(AWS_CREDENTIALS, clientConfig); s3Connection.setEndpoint(ConstantsAWS.S3_END_POINT); ObjectMetadata pictureMetadata = new ObjectMetadata(); String key = String.format(ConstantsAWS.S3_PICTURE_NAME_FORMAT, googleCredential.getSelectedAccountName(), Constants.DATE_FORMAT_IMAGE.format(new Date())); s3Connection.putObject(ConstantsAWS.S3_BUCKET_NAME, key, ImageUtils.bitmapToInputStream(picture), pictureMetadata); return new Key(key); } catch (Exception e) { Log.d(Constants.PROJECT_NAME, e.toString()); } return null; }
From source file:ch.myniva.gradle.caching.s3.internal.AwsS3BuildCacheService.java
License:Apache License
@Override public void store(BuildCacheKey key, BuildCacheEntryWriter writer) { final String bucketPath = getBucketPath(key); logger.info("Start storing cache entry '{}' in S3 bucket", bucketPath); ObjectMetadata meta = new ObjectMetadata(); meta.setContentType(BUILD_CACHE_CONTENT_TYPE); try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { writer.writeTo(os);//from w w w. ja v a 2s . co m meta.setContentLength(os.size()); try (InputStream is = new ByteArrayInputStream(os.toByteArray())) { PutObjectRequest request = getPutObjectRequest(bucketPath, meta, is); if (this.reducedRedundancy) { request.withStorageClass(StorageClass.ReducedRedundancy); } s3.putObject(request); } } catch (IOException e) { throw new BuildCacheException("Error while storing cache object in S3 bucket", e); } }
From source file:cloudExplorer.Put.java
License:Open Source License
public void run() { try {//from www . jav a2 s . c o m AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key); AmazonS3 s3Client = new AmazonS3Client(credentials, new ClientConfiguration().withSignerOverride("S3SignerType")); s3Client.setEndpoint(endpoint); TransferManager tx = new TransferManager(s3Client); File file = new File(what); PutObjectRequest putRequest; if (!rrs) { putRequest = new PutObjectRequest(bucket, ObjectKey, file); } else { putRequest = new PutObjectRequest(bucket, ObjectKey, file) .withStorageClass(StorageClass.ReducedRedundancy); } MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap(); String mimeType = mimeTypesMap.getContentType(file); mimeType = mimeTypesMap.getContentType(file); ObjectMetadata objectMetadata = new ObjectMetadata(); if (encrypt) { objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); } if ((ObjectKey.contains(".html")) || ObjectKey.contains(".txt")) { objectMetadata.setContentType("text/html"); } else { objectMetadata.setContentType(mimeType); } long t1 = System.currentTimeMillis(); putRequest.setMetadata(objectMetadata); Upload myUpload = tx.upload(putRequest); myUpload.waitForCompletion(); tx.shutdownNow(); long t2 = System.currentTimeMillis(); long diff = t2 - t1; if (!mainFrame.perf) { if (terminal) { System.out.print("\nUploaded object: " + ObjectKey + " in " + diff / 1000 + " second(s).\n"); } else { mainFrame.jTextArea1 .append("\nUploaded object: " + ObjectKey + " in " + diff / 1000 + " second(s)."); } } } catch (AmazonServiceException ase) { if (NewJFrame.gui) { mainFrame.jTextArea1.append("\n\nError Message: " + ase.getMessage()); mainFrame.jTextArea1.append("\nHTTP Status Code: " + ase.getStatusCode()); mainFrame.jTextArea1.append("\nAWS Error Code: " + ase.getErrorCode()); mainFrame.jTextArea1.append("\nError Type: " + ase.getErrorType()); mainFrame.jTextArea1.append("\nRequest ID: " + ase.getRequestId()); calibrate(); } else { System.out.print("\n\nError Message: " + ase.getMessage()); System.out.print("\nHTTP Status Code: " + ase.getStatusCode()); System.out.print("\nAWS Error Code: " + ase.getErrorCode()); System.out.print("\nError Type: " + ase.getErrorType()); System.out.print("\nRequest ID: " + ase.getRequestId()); } } catch (Exception put) { } calibrate(); }
From source file:com.adeptj.modules.aws.s3.internal.AwsS3Service.java
License:Apache License
/** * {@inheritDoc}// w w w.j a va2s .c o m */ @Override public S3Response createFolder(String bucketName, String folderName) { ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentLength(0); try { return new S3Response().withPutObjectResult(this.s3Client.putObject(new PutObjectRequest(bucketName, folderName + PATH_SEPARATOR, new ByteArrayInputStream(new byte[0]), objectMetadata))); } catch (RuntimeException ex) { LOGGER.error("Exception while creating folder!!", ex); throw new AwsException(ex.getMessage(), ex); } }
From source file:com.adeptj.modules.aws.s3.UploadResource.java
License:Apache License
@POST @Path(PATH_UPLOAD)/*from w w w .j ava2s. com*/ @Consumes(MULTIPART_FORM_DATA) @RequiresJwt public Response uploadFile(@MultipartForm S3UploadForm form) { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength((long) form.getData().length); this.storageService.uploadFile(S3Request.builder().bucketName(form.getBucketName()).key(form.getKey()) .data(new BufferedInputStream(new ByteArrayInputStream(form.getData()))).metadata(metadata) .cannedACL(CannedAccessControlList.valueOf(form.getAccess())).build()); return Response.ok("File uploaded successfully!!").build(); }
From source file:com.aegeus.aws.SimpleStorageService.java
License:Apache License
public void createFolder(String bucket, String key) { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(0);// w ww. j a v a 2s . c om InputStream nullObject = new ByteArrayInputStream(new byte[0]); PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key + "/", nullObject, metadata); s3.putObject(putObjectRequest); }