List of usage examples for com.amazonaws.services.s3.model ObjectMetadata setContentLength
public void setContentLength(long contentLength)
From source file:fr.eurecom.hybris.kvs.drivers.AmazonKvs.java
License:Apache License
public void put(String key, byte[] value) throws IOException { try {/*from w w w .j a v a 2s . c om*/ ByteArrayInputStream bais = new ByteArrayInputStream(value); ObjectMetadata om = new ObjectMetadata(); om.setContentLength(value.length); PutObjectRequest request = new PutObjectRequest(this.rootContainer, key, bais, om); request.setStorageClass(StorageClass.ReducedRedundancy); Upload upload = this.tm.upload(request); // NB: asynchronous, returns immediately upload.waitForCompletion(); } catch (AmazonClientException | InterruptedException e) { throw new IOException(e); } }
From source file:fr.xebia.cocktail.CocktailManager.java
License:Apache License
/** * TODO use PUT instead of POST//from w w w . ja va 2 s . c o m * * @param id id of the cocktail * @param photo to associate with the cocktail * @return redirection to display cocktail */ @RequestMapping(value = "/cocktail/{id}/photo", method = RequestMethod.POST) public String updatePhoto(@PathVariable String id, @RequestParam("photo") MultipartFile photo) { if (!photo.isEmpty()) { try { String contentType = fileStorageService.findContentType(photo.getOriginalFilename()); if (contentType == null) { logger.warn("photo", "Skip file with unsupported extension '" + photo.getOriginalFilename() + "'"); } else { InputStream photoInputStream = photo.getInputStream(); long photoSize = photo.getSize(); ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentLength(photoSize); objectMetadata.setContentType(contentType); objectMetadata .setCacheControl("public, max-age=" + TimeUnit.SECONDS.convert(365, TimeUnit.DAYS)); String photoUrl = fileStorageService.storeFile(photoInputStream, objectMetadata); Cocktail cocktail = cocktailRepository.get(id); logger.info("Saved {}", photoUrl); cocktail.setPhotoUrl(photoUrl); cocktailRepository.update(cocktail); } } catch (IOException e) { throw Throwables.propagate(e); } } return "redirect:/cocktail/" + id; }
From source file:fsi_admin.JAwsS3Conn.java
License:Open Source License
@SuppressWarnings("rawtypes") private boolean subirArchivo(StringBuffer msj, AmazonS3 s3, String S3BUKT, String nombre, Vector archivos) { //System.out.println("AwsConn SubirArchivo:" + nombre + ":nombre"); if (!archivos.isEmpty()) { FileItem actual = null;//from w ww. j a v a 2s.com try { for (int i = 0; i < archivos.size(); i++) { InputStream inputStream = null; try { actual = (FileItem) archivos.elementAt(i); ///////////////////////////////////////////////////////// //Obtain the Content length of the Input stream for S3 header InputStream is = actual.getInputStream(); byte[] contentBytes = IOUtils.toByteArray(is); Long contentLength = Long.valueOf(contentBytes.length); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(contentLength); //Reobtain the tmp uploaded file as input stream inputStream = actual.getInputStream(); //Put the object in S3 //System.out.println("BUCKET: " + S3BUKT + " OBJETO: " + nombre.replace('_', '-')); //System.out.println("BUCKET: " + S3BUKT + " OBJETO: " + nombre.replace('_', '-')); s3.putObject(new PutObjectRequest(S3BUKT, nombre, inputStream, metadata)); } finally { if (inputStream != null) try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } //////////////////////////////////////////////////////////// } return true; } catch (AmazonServiceException ase) { ase.printStackTrace(); msj.append("Error de AmazonServiceException al subir archivo a S3.<br>"); msj.append("Mensaje: " + ase.getMessage() + "<br>"); msj.append("Cdigo de Estatus HTTP: " + ase.getStatusCode() + "<br>"); msj.append("Cdigo de Error AWS: " + ase.getErrorCode() + "<br>"); msj.append("Tipo de Error: " + ase.getErrorType() + "<br>"); msj.append("Request ID: " + ase.getRequestId()); return false; } catch (AmazonClientException ace) { ace.printStackTrace(); msj.append("Error de AmazonClientException al subir archivo a S3.<br>"); msj.append("Mensaje: " + ace.getMessage()); return false; } catch (IOException e) { e.printStackTrace(); msj.append("Error de Entrada/Salida al subir archivo a S3: " + e.getMessage()); return false; } } else { msj.append("Error al subir archivo a la nube: No se envi ningun archivo"); return false; } }
From source file:gateway.controller.util.GatewayUtil.java
License:Apache License
/** * Handles the uploaded file from the data/file endpoint. This will push the file to S3, and then modify the content * of the job to reference the new S3 location of the file. * /*from w w w .j a v a 2s . c o m*/ * @param jobId * The Id of the Job, used for generating a unique S3 bucket file name. * @param job * The ingest job, containing the DataResource metadata * @param file * The file to be uploaded * @return The modified job, with the location of the S3 file added to the metadata */ public IngestJob pushS3File(String jobId, IngestJob job, MultipartFile file) throws AmazonServiceException, AmazonClientException, IOException { // The content length must be specified. ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(file.getSize()); // Send the file to S3. The key corresponds with the S3 file name. String fileKey = String.format("%s-%s", jobId, file.getOriginalFilename()); s3Client.putObject(AMAZONS3_BUCKET_NAME, fileKey, file.getInputStream(), metadata); // Note the S3 file path in the Ingest Job. // Attach the file to the FileLocation object FileLocation fileLocation = new S3FileStore(AMAZONS3_BUCKET_NAME, fileKey, file.getSize(), AMAZONS3_DOMAIN); ((FileRepresentation) job.getData().getDataType()).setLocation(fileLocation); logger.log(String.format("S3 File for Job %s Persisted to %s:%s", jobId, AMAZONS3_BUCKET_NAME, fileKey), PiazzaLogger.INFO); return job; }
From source file:generator.components.S3FileUtility.java
License:Apache License
/** * Upload file to s3 bucket//from w ww .j ava2s .c o m * * @param file * the object */ public String writeFileToS3(File file, FileLocation fileLocation) throws FileNotFoundException { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(file.length()); String fileKey = String.format("%s-%s", uuidFactory.getUUID(), file.getName()); // BasicAWSCredentials credentials = new BasicAWSCredentials(AMAZONS3_ACCESS_KEY, AMAZONS3_PRIVATE_KEY); s3Client = new AmazonS3Client(); // Making the object public PutObjectRequest putObj = new PutObjectRequest(S3_OUTPUT_BUCKET, fileKey, file); putObj.setCannedAcl(CannedAccessControlList.PublicRead); s3Client.putObject(putObj); return fileKey; }
From source file:gov.cdc.sdp.cbr.aphl.AphlS3Producer.java
License:Apache License
public void processMultiPart(final Exchange exchange) throws Exception { File filePayload = null;//ww w.j a v a2 s . co m Object obj = exchange.getIn().getMandatoryBody(); // Need to check if the message body is WrappedFile if (obj instanceof WrappedFile) { obj = ((WrappedFile<?>) obj).getFile(); } if (obj instanceof File) { filePayload = (File) obj; } else { LOG.error("aphl-s3: MultiPart upload requires a File input."); throw new InvalidArgumentException("aphl-s3: MultiPart upload requires a File input."); } ObjectMetadata objectMetadata = determineMetadata(exchange); if (objectMetadata.getContentLength() == 0) { objectMetadata.setContentLength(filePayload.length()); } final String keyName = determineKey(exchange); final InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest( getConfiguration().getBucketName(), keyName, objectMetadata); String storageClass = determineStorageClass(exchange); if (storageClass != null) { initRequest.setStorageClass(StorageClass.fromValue(storageClass)); } String cannedAcl = exchange.getIn().getHeader(S3Constants.CANNED_ACL, String.class); if (cannedAcl != null) { CannedAccessControlList objectAcl = CannedAccessControlList.valueOf(cannedAcl); initRequest.setCannedACL(objectAcl); } AccessControlList acl = exchange.getIn().getHeader(S3Constants.ACL, AccessControlList.class); if (acl != null) { // note: if cannedacl and acl are both specified the last one will // be used. refer to // PutObjectRequest#setAccessControlList for more details initRequest.setAccessControlList(acl); } LOG.trace("Initiating multipart upload ..."); final InitiateMultipartUploadResult initResponse = getEndpoint().getS3Client() .initiateMultipartUpload(initRequest); final long contentLength = objectMetadata.getContentLength(); final List<PartETag> partETags = new ArrayList<PartETag>(); long partSize = getConfiguration().getPartSize(); CompleteMultipartUploadResult uploadResult = null; long filePosition = 0; try { for (int part = 1; filePosition < contentLength; part++) { partSize = Math.min(partSize, contentLength - filePosition); UploadPartRequest uploadRequest = new UploadPartRequest() .withBucketName(getConfiguration().getBucketName()).withKey(keyName) .withUploadId(initResponse.getUploadId()).withPartNumber(part).withFileOffset(filePosition) .withFile(filePayload).withPartSize(partSize); partETags.add(getEndpoint().getS3Client().uploadPart(uploadRequest).getPartETag()); filePosition += partSize; } CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest( getConfiguration().getBucketName(), keyName, initResponse.getUploadId(), partETags); uploadResult = getEndpoint().getS3Client().completeMultipartUpload(compRequest); } catch (Exception exception) { LOG.error("Multi-part upload failed, aborting", exception); getEndpoint().getS3Client().abortMultipartUpload(new AbortMultipartUploadRequest( getConfiguration().getBucketName(), keyName, initResponse.getUploadId())); throw exception; } Message message = getMessageForResponse(exchange); message.setHeader(S3Constants.E_TAG, uploadResult.getETag()); if (uploadResult.getVersionId() != null) { message.setHeader(S3Constants.VERSION_ID, uploadResult.getVersionId()); } if (getConfiguration().isDeleteAfterWrite() && filePayload != null) { FileUtil.deleteFile(filePayload); } }
From source file:gov.cdc.sdp.cbr.aphl.AphlS3Producer.java
License:Apache License
private ObjectMetadata determineMetadata(final Exchange exchange) { ObjectMetadata objectMetadata = new ObjectMetadata(); Long contentLength = exchange.getIn().getHeader(S3Constants.CONTENT_LENGTH, Long.class); if (contentLength != null) { objectMetadata.setContentLength(contentLength); }//from w w w . ja v a 2 s. c om String contentType = exchange.getIn().getHeader(S3Constants.CONTENT_TYPE, String.class); if (contentType != null) { objectMetadata.setContentType(contentType); } String cacheControl = exchange.getIn().getHeader(S3Constants.CACHE_CONTROL, String.class); if (cacheControl != null) { objectMetadata.setCacheControl(cacheControl); } String contentDisposition = exchange.getIn().getHeader(S3Constants.CONTENT_DISPOSITION, String.class); if (contentDisposition != null) { objectMetadata.setContentDisposition(contentDisposition); } String contentEncoding = exchange.getIn().getHeader(S3Constants.CONTENT_ENCODING, String.class); if (contentEncoding != null) { objectMetadata.setContentEncoding(contentEncoding); } String contentMD5 = exchange.getIn().getHeader(S3Constants.CONTENT_MD5, String.class); if (contentMD5 != null) { objectMetadata.setContentMD5(contentMD5); } Date lastModified = exchange.getIn().getHeader(S3Constants.LAST_MODIFIED, Date.class); if (lastModified != null) { objectMetadata.setLastModified(lastModified); } Map<String, String> userMetadata = CastUtils .cast(exchange.getIn().getHeader(S3Constants.USER_METADATA, Map.class)); if (userMetadata != null) { objectMetadata.setUserMetadata(userMetadata); } Map<String, String> s3Headers = CastUtils .cast(exchange.getIn().getHeader(S3Constants.S3_HEADERS, Map.class)); if (s3Headers != null) { for (Map.Entry<String, String> entry : s3Headers.entrySet()) { objectMetadata.setHeader(entry.getKey(), entry.getValue()); } } String encryption = exchange.getIn().getHeader(S3Constants.SERVER_SIDE_ENCRYPTION, getConfiguration().getServerSideEncryption(), String.class); if (encryption != null) { objectMetadata.setSSEAlgorithm(encryption); } return objectMetadata; }
From source file:gov.usgs.cida.iplover.util.ImageStorage.java
public static String save(byte[] parsedImage, String uuid) throws IOException { AmazonS3 s3 = prepS3Client();//w ww. j a va2 s.com ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(parsedImage.length); LOG.trace("Setting up image key."); //Build key, split by date uploaded Date d = new Date(); String fname = uuid + ".jpg"; String fileId = simp.format(d) + "/" + fname; String fileKey = KEY_BASE + "/" + fileId; s3.putObject(BUCKET_NAME, fileKey, new ByteArrayInputStream(parsedImage), metadata); LOG.trace("Image uploaded."); //The date directory and filename are the "unique key" return (fileId); }
From source file:gr.abiss.calipso.fs.S3FilePersistenceServiceImpl.java
License:Open Source License
/** * Save file in S3/* w w w .j ava 2 s .c o m*/ * @see gr.abiss.calipso.fs.FilePersistenceService#saveFile(java.io.InputStream, long, java.lang.String, java.lang.String) */ @Override public String saveFile(InputStream in, long contentLength, String contentType, String path) { String url; // create metadata ObjectMetadata meta = new ObjectMetadata(); meta.setContentLength(contentLength); meta.setContentType(contentType); // save to bucket s3Client.putObject(new PutObjectRequest(nameCardBucket, path, in, meta) .withCannedAcl(CannedAccessControlList.PublicRead)); // set the URL to return url = s3Client.getUrl(nameCardBucket, path).toString(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("File saved: " + path + ", size: " + contentLength + ", contentType: " + contentType); } return url; }
From source file:io.jeffrey.web.assemble.S3PutObjectTarget.java
@Override public void upload(String key, String md5, String contentType, InputStream body, long contentLength) throws Exception { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(contentLength); metadata.setContentType(contentType); if (md5.equalsIgnoreCase(etags.get(key))) { System.out.println("skipping:" + key); return;/*from ww w .j ava 2s. c o m*/ } System.out.println("uploading:" + key); s3.putObject(new PutObjectRequest(bucket, key, body, metadata) .withCannedAcl(CannedAccessControlList.PublicRead)); }