List of usage examples for com.amazonaws.services.s3.model ObjectMetadata setContentType
public void setContentType(String contentType)
From source file:alluxio.underfs.s3a.S3ALowLevelOutputStream.java
License:Apache License
/** * Initializes multipart upload.//w ww . j ava 2 s .c o m */ private void initMultiPartUpload() throws IOException { // Generate the object metadata by setting server side encryption, md5 checksum, // and encoding as octet stream since no assumptions are made about the file type ObjectMetadata meta = new ObjectMetadata(); if (mSseEnabled) { meta.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); } if (mHash != null) { meta.setContentMD5(Base64.encodeAsString(mHash.digest())); } meta.setContentType(Mimetypes.MIMETYPE_OCTET_STREAM); AmazonClientException lastException; InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(mBucketName, mKey) .withObjectMetadata(meta); do { try { mUploadId = mClient.initiateMultipartUpload(initRequest).getUploadId(); return; } catch (AmazonClientException e) { lastException = e; } } while (mRetryPolicy.attempt()); // This point is only reached if the operation failed more // than the allowed retry count throw new IOException("Unable to init multipart upload to " + mKey, lastException); }
From source file:alluxio.underfs.s3a.S3AUnderFileSystem.java
License:Apache License
/** * Creates a directory flagged file with the key and folder suffix. * * @param key the key to create a folder * @return true if the operation was successful, false otherwise *//*from w ww.j a v a 2 s . c o m*/ private boolean mkdirsInternal(String key) { try { String keyAsFolder = convertToFolderName(stripPrefixIfPresent(key)); ObjectMetadata meta = new ObjectMetadata(); meta.setContentLength(0); meta.setContentMD5(DIR_HASH); meta.setContentType(Mimetypes.MIMETYPE_OCTET_STREAM); mClient.putObject( new PutObjectRequest(mBucketName, keyAsFolder, new ByteArrayInputStream(new byte[0]), meta)); return true; } catch (AmazonClientException e) { LOG.error("Failed to create directory: {}", key, e); return false; } }
From source file:biz.k11i.S3GyazoController.java
License:Open Source License
@RequestMapping(value = "/upload.cgi", method = RequestMethod.POST) @ResponseBody//from ww w .j av a2 s .co m String upload(@RequestParam("imagedata") MultipartFile imagedata) throws IOException { if (imagedata.isEmpty()) { String message = "????????"; logger.warn(message); throw new BadRequestException(message); } byte[] bytes = imagedata.getBytes(); String hash = generateHash(bytes); String filename = String.format("%s.png", hash); try (InputStream input = imagedata.getInputStream()) { ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentType("image/png"); objectMetadata.setContentLength(bytes.length); PutObjectRequest req = new PutObjectRequest(bucket, filename, input, objectMetadata) .withCannedAcl(CannedAccessControlList.PublicRead); amazonS3Client.putObject(req); } String result = urlPrefix + filename; logger.info("New image uploaded {}", result); return result; }
From source file:ca.pgon.amazons3masscontenttype.App.java
License:Apache License
private static void process(ObjectListing objectListing) { for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { // Show the key String key = objectSummary.getKey(); System.out.println(key);/*from w w w . j av a 2 s. com*/ // Get the metadata and check the content type ObjectMetadata objectMetadata = amazonS3Client.getObjectMetadata(bucketName, key); System.out.println("\tCurrent content type: " + objectMetadata.getContentType()); if (!contentType.equals(objectMetadata.getContentType())) { System.out.println("\tChanging content type for : " + contentType); objectMetadata.setContentType(contentType); // Get the current ACL AccessControlList accessControlList = amazonS3Client.getObjectAcl(bucketName, key); // Modify the file CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucketName, key, bucketName, key); copyObjectRequest.withNewObjectMetadata(objectMetadata); copyObjectRequest.withAccessControlList(accessControlList); amazonS3Client.copyObject(copyObjectRequest); } ++count; } }
From source file:ch.admin.isb.hermes5.persistence.s3.S3RemoteAdapter.java
License:Apache License
@Override @Asynchronous//from ww w . j a v a2s . c om @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.entwine.weblounge.maven.S3DeployMojo.java
License:Open Source License
/** * /*from w w w . j a v a 2s .com*/ * {@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.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 ww . j a v a2 s.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 w ww. ja v a 2 s. c om*/ 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.ALC.SC2BOAserver.aws.S3StorageManager.java
License:Open Source License
/** * Stores a given item on S3//from ww w . j a v a 2s.com * @param obj the data to be stored * @param reducedRedundancy whether or not to use reduced redundancy storage * @param acl a canned access control list indicating what permissions to store this object with (can be null to leave it set to default) */ public void store(SC2BOAStorageObject obj, boolean reducedRedundancy, CannedAccessControlList acl) { // Make sure the bucket exists before we try to use it checkForAndCreateBucket(obj.getBucketName()); ObjectMetadata omd = new ObjectMetadata(); omd.setContentType(obj.getMimeType()); omd.setContentLength(obj.getData().length); ByteArrayInputStream is = new ByteArrayInputStream(obj.getData()); PutObjectRequest request = new PutObjectRequest(obj.getBucketName(), obj.getStoragePath(), is, omd); // Check if reduced redundancy is enabled if (reducedRedundancy) { request.setStorageClass(StorageClass.ReducedRedundancy); } s3Client.putObject(request); // If we have an ACL set access permissions for the the data on S3 if (acl != null) { s3Client.setObjectAcl(obj.getBucketName(), obj.getStoragePath(), acl); } }
From source file:com.amazon.aws.samplecode.travellog.aws.S3StorageManager.java
License:Open Source License
/** * Stores a given item on S3//from w w w. j a v a 2 s .c om * @param obj the data to be stored * @param reducedRedundancy whether or not to use reduced redundancy storage * @param acl a canned access control list indicating what permissions to store this object with (can be null to leave it set to default) */ public void store(TravelLogStorageObject obj, boolean reducedRedundancy, CannedAccessControlList acl) { //Make sure the bucket exists before we try to use it checkForAndCreateBucket(obj.getBucketName()); ObjectMetadata omd = new ObjectMetadata(); omd.setContentType(obj.getMimeType()); omd.setContentLength(obj.getData().length); ByteArrayInputStream is = new ByteArrayInputStream(obj.getData()); PutObjectRequest request = new PutObjectRequest(obj.getBucketName(), obj.getStoragePath(), is, omd); //Check if reduced redundancy is enabled if (reducedRedundancy) { request.setStorageClass(StorageClass.ReducedRedundancy); } s3client.putObject(request); //If we have an ACL set access permissions for the the data on S3 if (acl != null) { s3client.setObjectAcl(obj.getBucketName(), obj.getStoragePath(), acl); } }