List of usage examples for com.amazonaws.services.s3.model ObjectMetadata setContentLength
public void setContentLength(long contentLength)
From source file:org.openflamingo.fs.s3.S3ObjectProvider.java
License:Apache License
@Override public boolean mkdir(String path) { // Assert.hasLength(path, " ? 'path' ."); Assert.hasLength(path, "Please enter the path"); String bucket = S3Utils.getBucket(path); String relativePath = S3Utils.getObjectKey(path); try {//from w ww. j a v a2 s . c o m ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(0); InputStream emptyContent = new ByteArrayInputStream(new byte[0]); PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, relativePath, emptyContent, metadata); awsClient.putObject(putObjectRequest); /* auditService.mkdir(FileSystemType.S3, username, path); */ return true; } catch (AmazonServiceException ase) { // throw new FileSystemException(" ? ? . ? ? ? .", ase); throw new FileSystemException("Cannot create the directory.", ase); } catch (AmazonClientException ace) { // throw new FileSystemException(" ? ? . ? ? ? .", ace); throw new FileSystemException("Cannot create the directory.", ace); } }
From source file:org.pieShare.pieDrive.adapter.s3.S3Adapter.java
@Override public synchronized void upload(PieDriveFile file, InputStream stream) throws AdaptorException { try {//from w ww . j ava2 s. com ObjectMetadata meta = new ObjectMetadata(); meta.setContentLength(file.getSize()); PutObjectRequest req = new PutObjectRequest(bucketName, file.getUuid(), stream, meta); //req.getRequestClientOptions().setReadLimit(64); s3Auth.getClient().putObject(req); //Thread.sleep(2000); PieLogger.trace(S3Adapter.class, "{} uploaded", file.getUuid()); } catch (AmazonServiceException ase) { throw new AdaptorException(ase); } catch (AmazonClientException ace) { throw new AdaptorException(ace); } catch (Exception e) { } }
From source file:org.plos.repo.service.S3StoreService.java
License:Open Source License
@Override public boolean saveUploadedObject(Bucket bucket, UploadInfo uploadInfo, RepoObject repoObject) { int retries = 5; int tryCount = 0; int waitSecond = 4; ObjectMapper m = new ObjectMapper(); Map<String, java.lang.Object> propsObj = m.convertValue(repoObject, Map.class); Map<String, String> propsStr = new HashMap<>(); for (Map.Entry<String, java.lang.Object> entry : propsObj.entrySet()) { try {// ww w . j ava 2s . co m if (entry.getValue() == null) { propsStr.put(entry.getKey(), ""); } else { propsStr.put(entry.getKey(), entry.getValue().toString()); } } catch (ClassCastException cce) { log.error("Problem converting object to metadata", cce); } } ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentLength(uploadInfo.getSize()); objectMetadata.setUserMetadata(propsStr); File tempFile = new File(uploadInfo.getTempLocation()); PutObjectRequest putObjectRequest = new PutObjectRequest(bucket.getBucketName(), uploadInfo.getChecksum(), tempFile); putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); putObjectRequest.setMetadata(objectMetadata); while (tryCount < retries) { try { s3Client.putObject(putObjectRequest); // TODO: check result and do something about it tempFile.delete(); return true; } catch (Exception e) { tryCount++; log.error("Error during putObject", e); try { Thread.sleep(waitSecond * 1000); } catch (Exception e2) { } } } return false; }
From source file:org.rdswitchboard.harvesters.pmh.Harvester.java
License:Open Source License
public void downloadRecords(SetStatus set) throws HarvesterException, UnsupportedEncodingException, IOException, InterruptedException, XPathExpressionException, SAXException, ParserConfigurationException { // Generate the URL of request String url = null;/* www .ja va 2 s . c o m*/ ; if (set.hasToken()) { try { url = repoUrl + String.format(URL_LIST_RECORDS_RESUMPTION_TOKEN, URLEncoder.encode(set.getToken(), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } if (null == url) { if (!set.hasName()) url = repoUrl + String.format(URL_LIST_DEFAULT_RECORDS, metadataPrefix); else url = repoUrl + String.format(URL_LIST_RECORDS, URLEncoder.encode(set.getName(), "UTF-8"), metadataPrefix); } System.out.println("Downloading records: " + url); String xml = null; // Get XML document URLConnection conn = new URL(url).openConnection(); if (connectionTimeout > 0) conn.setConnectTimeout(connectionTimeout); if (readTimeout > 0) conn.setReadTimeout(readTimeout); try (InputStream is = conn.getInputStream()) { if (null != is) xml = IOUtils.toString(is, StandardCharsets.UTF_8.name()); } // Check if xml has been returned and check what it had a valid root element if (null == xml) throw new HarvesterException("The XML document is empty"); Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xml))); // Extract root node Node root = (Node) XPATH_OAI_PMH.evaluate(doc, XPathConstants.NODE); if (null == root) throw new HarvesterException("The document is not an OAI:PMH file"); // Check for error node Node error = (Node) XPATH_ERROR.evaluate(root, XPathConstants.NODE); if (null != error && error instanceof Element) { String code = ((Element) error).getAttribute("code"); String message = ((Element) error).getTextContent(); if (ERR_NO_RECORDS_MATCH.equals(code)) { System.out.println("Error: The set is empty"); set.setFiles(0); set.resetToken(); return; } else throw new HarvesterException(code, message); } Node nodeToken = (Node) XPATH_RECORDS_RESUMPTION_TOKEN.evaluate(root, XPathConstants.NODE); if (null != nodeToken && nodeToken instanceof Element) { String tokenString = ((Element) nodeToken).getTextContent(); if (null != tokenString && !tokenString.isEmpty()) set.setToken(tokenString); else set.resetToken(); set.setCursor(((Element) nodeToken).getAttribute("cursor")); set.setSize(((Element) nodeToken).getAttribute("completeListSize")); set.dumpToken(System.out); } else set.resetToken(); String filePath = repoPrefix + "/" + metadataPrefix + "/" + harvestDate + "/" + set.getNameSafe() + "/" + set.getFiles() + ".xml"; if (StringUtils.isNullOrEmpty(bucketName)) { FileUtils.writeStringToFile(new File(folderName, filePath), xml); } else { byte[] bytes = xml.getBytes(StandardCharsets.UTF_8); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentEncoding(StandardCharsets.UTF_8.name()); metadata.setContentType("text/xml"); metadata.setContentLength(bytes.length); InputStream inputStream = new ByteArrayInputStream(bytes); PutObjectRequest request = new PutObjectRequest(bucketName, filePath, inputStream, metadata); s3client.putObject(request); } set.incFiles(); }
From source file:org.rdswitchboard.harvesters.pmh.Harvester.java
License:Open Source License
/** * Alternative function to organize the harvest process. The difference with another function * is in data storage. The harvest2 function will store files in the raw format as they come * from the server.//from www. ja va2 s.c o m * The harvesting method should never be mixed. The harvesting folder must be wiped out if * switching to this method, or function will fail. * @param prefix A metadata prefix * @throws Exception */ public boolean harvest() throws Exception { if (StringUtils.isNullOrEmpty(metadataPrefix)) throw new IllegalArgumentException("The OAI:PMH Metadata Prefix can not be empty"); System.out.println("Downloading set list"); boolean result = false; if (null == whiteList || whiteList.isEmpty()) { System.out.println( "There is no whitelist found. Proceeding with downloading the list of all available sets."); // download all sets in the repository Map<String, String> mapSets = listSets(); if (null == mapSets || mapSets.isEmpty()) { System.out.println("Processing default set"); result = harvestSet(new SetStatus(null, "Default")); } else { result = false; for (Map.Entry<String, String> entry : mapSets.entrySet()) { SetStatus set = new SetStatus(entry.getKey().trim(), URLDecoder.decode(entry.getValue(), StandardCharsets.UTF_8.name())); // if black list exists and item is blacklisted, continue if (null != blackList && blackList.contains(set)) { set.setFiles(-2); saveSetStats(set); // set was ignored continue; } System.out.println("Processing set: " + URLDecoder.decode(entry.getValue(), StandardCharsets.UTF_8.name())); if (!harvestSet(set)) { System.err.println( "The harvesting job has been aborted due to an error. If you want harvesting to be continued, please set option 'fail.on.error' to 'false' in the configuration file"); result = false; break; } else result = true; } } } else { for (String item : whiteList) { if (!harvestSet(new SetStatus(item, item))) { System.err.println( "The harvesting job has been aborted due to an error. If you want harvesting to be continued, please set option 'fail.on.error' to 'false' in the configuration file"); result = false; break; } else result = true; } } if (result) { String filePath = repoPrefix + "/" + metadataPrefix + "/latest.txt"; if (StringUtils.isNullOrEmpty(bucketName)) { FileUtils.writeStringToFile(new File(folderName, filePath), harvestDate); } else { byte[] bytes = harvestDate.getBytes(StandardCharsets.UTF_8); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentEncoding(StandardCharsets.UTF_8.name()); metadata.setContentType("text/plain"); metadata.setContentLength(bytes.length); InputStream inputStream = new ByteArrayInputStream(bytes); PutObjectRequest request = new PutObjectRequest(bucketName, filePath, inputStream, metadata); s3client.putObject(request); } } return result; }
From source file:org.researchgraph.crossref.CrossRef.java
private void saveCacheFile(String file, String json) throws IOException { if (null != file && null != json && !json.isEmpty()) { if (null != cache) { FileUtils.write(new File(cache, file), json); } else if (null != s3Client) { byte[] bytes = json.getBytes(StandardCharsets.UTF_8); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentEncoding(StandardCharsets.UTF_8.name()); metadata.setContentType("text/json"); metadata.setContentLength(bytes.length); InputStream inputStream = new ByteArrayInputStream(bytes); s3Client.putObject(new PutObjectRequest(s3Bucket, getS3Key(file), inputStream, metadata)); }/*from ww w . ja v a2 s . c o m*/ } }
From source file:org.serginho.awss3conn.Connection.java
public boolean updaloadFile(UploadedFile uploadedFile) { ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentLength(uploadedFile.getSize()); try {/*from w w w .j a v a2s.c o m*/ getS3Client().putObject(this.amazonBucket, this.key + uploadedFile.getFileName(), uploadedFile.getInputstream(), objectMetadata); } catch (IOException ex) { Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex); return false; } return true; }
From source file:org.springframework.build.aws.maven.PrivateS3Wagon.java
License:Apache License
private PutObjectRequest createDirectoryPutObjectRequest(String key) { ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]); ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentLength(0); return new PutObjectRequest(this.bucketName, key, inputStream, objectMetadata); }
From source file:org.springframework.integration.aws.outbound.S3MessageHandler.java
License:Apache License
private Transfer upload(Message<?> requestMessage) { Object payload = requestMessage.getPayload(); String bucketName = obtainBucket(requestMessage); String key = null;//from ww w.jav a 2 s. c o m if (this.keyExpression != null) { key = this.keyExpression.getValue(this.evaluationContext, requestMessage, String.class); } if (payload instanceof File && ((File) payload).isDirectory()) { File fileToUpload = (File) payload; if (key == null) { key = fileToUpload.getName(); } return this.transferManager.uploadDirectory(bucketName, key, fileToUpload, true, new MessageHeadersObjectMetadataProvider(requestMessage.getHeaders())); } else { ObjectMetadata metadata = new ObjectMetadata(); if (this.uploadMetadataProvider != null) { this.uploadMetadataProvider.populateMetadata(metadata, requestMessage); } InputStream inputStream; if (payload instanceof InputStream) { inputStream = (InputStream) payload; } else if (payload instanceof File) { File fileToUpload = (File) payload; if (key == null) { key = fileToUpload.getName(); } try { inputStream = new FileInputStream(fileToUpload); if (metadata.getContentLength() == 0) { metadata.setContentLength(fileToUpload.length()); } if (metadata.getContentType() == null) { metadata.setContentType(Mimetypes.getInstance().getMimetype(fileToUpload)); } } catch (FileNotFoundException e) { throw new AmazonClientException(e); } } else if (payload instanceof byte[]) { inputStream = new ByteArrayInputStream((byte[]) payload); } else { throw new IllegalArgumentException("Unsupported payload type: [" + payload.getClass() + "]. The only supported payloads for the upload request are " + "java.io.File, java.io.InputStream, byte[] and PutObjectRequest."); } Assert.state(key != null, "The 'keyExpression' must not be null for non-File payloads and can't evaluate to null. " + "Root object is: " + requestMessage); if (key == null) { if (this.keyExpression != null) { throw new IllegalStateException( "The 'keyExpression' [" + this.keyExpression.getExpressionString() + "] must not evaluate to null. Root object is: " + requestMessage); } else { throw new IllegalStateException("Specify a 'keyExpression' for non-java.io.File payloads"); } } if (metadata.getContentMD5() == null) { String contentMd5 = null; try { contentMd5 = Md5Utils.md5AsBase64(StreamUtils.copyToByteArray(inputStream)); if (inputStream.markSupported()) { inputStream.reset(); } metadata.setContentMD5(contentMd5); } catch (IOException e) { throw new MessageHandlingException(requestMessage, e); } } PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, metadata); S3ProgressListener progressListener = this.s3ProgressListener; if (this.objectAclExpression != null) { Object acl = this.objectAclExpression.getValue(this.evaluationContext, requestMessage); Assert.state(acl instanceof AccessControlList || acl instanceof CannedAccessControlList, "The 'objectAclExpression' [" + this.objectAclExpression.getExpressionString() + "] must evaluate to com.amazonaws.services.s3.model.AccessControlList " + "or must evaluate to com.amazonaws.services.s3.model.CannedAccessControlList. " + "Gotten: [" + acl + "]"); SetObjectAclRequest aclRequest; if (acl instanceof AccessControlList) { aclRequest = new SetObjectAclRequest(bucketName, key, (AccessControlList) acl); } else { aclRequest = new SetObjectAclRequest(bucketName, key, (CannedAccessControlList) acl); } final SetObjectAclRequest theAclRequest = aclRequest; progressListener = new S3ProgressListener() { @Override public void onPersistableTransfer(PersistableTransfer persistableTransfer) { } @Override public void progressChanged(ProgressEvent progressEvent) { if (ProgressEventType.TRANSFER_COMPLETED_EVENT.equals(progressEvent.getEventType())) { S3MessageHandler.this.transferManager.getAmazonS3Client().setObjectAcl(theAclRequest); } } }; if (this.s3ProgressListener != null) { progressListener = new S3ProgressListenerChain(this.s3ProgressListener, progressListener); } } if (progressListener != null) { return this.transferManager.upload(putObjectRequest, progressListener); } else { return this.transferManager.upload(putObjectRequest); } } }
From source file:org.symphonyoss.vb.util.AwsS3Client.java
License:Apache License
public void putObject(String destBucket, String key, InputStream inputStream, ObjectMetadata metaData) { try {//from w w w. jav a 2s . c om logger.info("Put object for s3://{}/{}", destBucket, key); byte[] bytes = IOUtils.toByteArray(inputStream); if (metaData == null) metaData = new ObjectMetadata(); metaData.setContentLength(bytes.length); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); s3Client.putObject(new PutObjectRequest(destBucket, key, byteArrayInputStream, metaData)); } catch (AmazonServiceException ase) { logger.error("Caught an AmazonServiceException, " + "which means your request made it " + "to Amazon S3, but was rejected with an error response " + "for some reason."); logger.error("Error Message: " + ase.getMessage()); logger.error("HTTP Status Code: " + ase.getStatusCode()); logger.error("AWS Error Code: " + ase.getErrorCode()); logger.error("Error Type: " + ase.getErrorType()); logger.error("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { logger.error("Caught an AmazonClientException, " + "which means the client encountered " + "an internal error while trying to communicate" + " with S3, " + "such as not being able to access the network."); logger.error("Error Message: " + ace.getMessage()); } catch (IOException e) { logger.error("Obtaining length", e); } }