本文整理汇总了Java中com.amazonaws.services.s3.transfer.TransferManager.upload方法的典型用法代码示例。如果您正苦于以下问题:Java TransferManager.upload方法的具体用法?Java TransferManager.upload怎么用?Java TransferManager.upload使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.amazonaws.services.s3.transfer.TransferManager
的用法示例。
在下文中一共展示了TransferManager.upload方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: uploadToS3
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
private String uploadToS3(String bucket, String key, MultipartFile file) {
final AmazonS3 s3 = s3ClientFactory.createClient();
final TransferManager transferManager = TransferManagerBuilder.standard().withS3Client(s3).build();
try {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(file.getSize());
metadata.setContentType(file.getContentType());
byte[] resultByte = DigestUtils.md5(file.getBytes());
String streamMD5 = new String(Base64.encodeBase64(resultByte));
metadata.setContentMD5(streamMD5);
Upload upload = transferManager.upload(bucket, key, file.getInputStream(), metadata);
upload.waitForCompletion();
return streamMD5;
} catch (AmazonServiceException | InterruptedException | IOException e) {
logger.error("Error uploading file: {}", e.toString());
return null;
} finally {
transferManager.shutdownNow();
}
}
示例2: shouldUploadInParallel
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
/**
* Tests if an object can be uploaded asynchronously
*
* @throws Exception not expected
*/
@Test
public void shouldUploadInParallel() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final TransferManager transferManager = createDefaultTransferManager();
final Upload upload =
transferManager.upload(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile));
final UploadResult uploadResult = upload.waitForUploadResult();
assertThat(uploadResult.getKey(), equalTo(UPLOAD_FILE_NAME));
final S3Object getResult = s3Client.getObject(BUCKET_NAME, UPLOAD_FILE_NAME);
assertThat(getResult.getKey(), equalTo(UPLOAD_FILE_NAME));
}
示例3: checkRangeDownloads
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
/**
* Verify that range-downloads work.
*
* @throws Exception not expected
*/
@Test
public void checkRangeDownloads() throws Exception {
final File uploadFile = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
final TransferManager transferManager = createDefaultTransferManager();
final Upload upload =
transferManager.upload(new PutObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME, uploadFile));
upload.waitForUploadResult();
final File downloadFile = File.createTempFile(UUID.randomUUID().toString(), null);
transferManager
.download(new GetObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME).withRange(1, 2),
downloadFile)
.waitForCompletion();
assertThat("Invalid file length", downloadFile.length(), is(2L));
transferManager
.download(new GetObjectRequest(BUCKET_NAME, UPLOAD_FILE_NAME).withRange(0, 1000),
downloadFile)
.waitForCompletion();
assertThat("Invalid file length", downloadFile.length(), is(uploadFile.length()));
}
示例4: uploadArtifactStream
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
private void uploadArtifactStream(IndexArtifact ia, StorageRequest sr) throws LocalStorageException
{
try
{
TransferManager tx = new TransferManager(client);
ObjectMetadata om = new ObjectMetadata();
om.setContentLength(sr.getLength());
String key = getPath() + ia.getLocation() + "/" + sr.getFilename();
Upload myUpload = tx.upload(bucketName, key, sr.getNewStream(), om);
myUpload.waitForCompletion();
}
catch (Exception exc)
{
logger.error(exc.getLocalizedMessage());
throw new LocalStorageException(exc);
}
}
示例5: uploadDirectoryOrFile
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
@Override
public Transfer uploadDirectoryOrFile(final String bucketName, final File source,
final String virtualDirectoryKeyPrefix) throws AmazonClientException, AmazonServiceException, IOException {
LOGGER.info("uploadDirectoryOrFile invoked, bucketName: {} , Source: {}", bucketName,
source.getAbsolutePath());
Transfer transfer = null;
final TransferManager trMgr = new TransferManager(s3client);
if (source.isFile()) {
transfer = trMgr.upload(bucketName,source.getPath(),source);
} else if (source.isDirectory()) {
//Upload recursively
//virtualDirectoryKeyPrefix could be virtual directory name inside the bucket
transfer = trMgr.uploadDirectory(bucketName, virtualDirectoryKeyPrefix, source, true);
} else {
throw new FileNotFoundException("Source is neither a regular file nor a directory " + source);
}
return transfer;
}
示例6: call
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
@Override
public Integer call() throws Exception {
TransferManager t = new TransferManager(amazonS3Client);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setUserMetadata(metadata);
if(sse) {
objectMetadata.setSSEAlgorithm(SSEAlgorithm.AES256.getAlgorithm());
}
Upload u = t.upload(new PutObjectRequest(bucket, key, inputFile).withMetadata(objectMetadata));
// TODO this listener spews out garbage >100% on a retry, add a test to verify
if (progressListener != null) {
progressListener.withTransferProgress(new TransferProgressWrapper(u.getProgress()));
u.addProgressListener(progressListener);
}
try {
u.waitForCompletion();
} finally {
t.shutdownNow();
}
return 0;
}
示例7: shouldUploadAndDownloadStream
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
/**
* Stores a file in a previously created bucket. Downloads the file again and compares checksums
*
* @throws Exception if FileStreams can not be read
*/
@Test
public void shouldUploadAndDownloadStream() throws Exception {
s3Client.createBucket(BUCKET_NAME);
final String resourceId = UUID.randomUUID().toString();
final byte[] resource = new byte[] {1, 2, 3, 4, 5};
final ByteArrayInputStream bais = new ByteArrayInputStream(resource);
final ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(resource.length);
final PutObjectRequest putObjectRequest =
new PutObjectRequest(BUCKET_NAME, resourceId, bais, objectMetadata);
final TransferManager tm = createDefaultTransferManager();
final Upload upload = tm.upload(putObjectRequest);
upload.waitForUploadResult();
final S3Object s3Object = s3Client.getObject(BUCKET_NAME, resourceId);
final String uploadHash = HashUtil.getDigest(new ByteArrayInputStream(resource));
final String downloadedHash = HashUtil.getDigest(s3Object.getObjectContent());
s3Object.close();
assertThat("Up- and downloaded Files should have equal Hashes", uploadHash,
is(equalTo(downloadedHash)));
}
示例8: multipartCopy
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
/**
* Verifies multipart copy.
*
* @throws InterruptedException
*/
@Test
public void multipartCopy() throws InterruptedException, IOException, NoSuchAlgorithmException {
final int contentLen = 3 * _1MB;
final ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(contentLen);
final String assumedSourceKey = UUID.randomUUID().toString();
final Bucket sourceBucket = s3Client.createBucket(UUID.randomUUID().toString());
final Bucket targetBucket = s3Client.createBucket(UUID.randomUUID().toString());
final TransferManager transferManager = createTransferManager(_2MB, _1MB, _2MB, _1MB);
final InputStream sourceInputStream = randomInputStream(contentLen);
final Upload upload = transferManager
.upload(sourceBucket.getName(), assumedSourceKey,
sourceInputStream, objectMetadata);
final UploadResult uploadResult = upload.waitForUploadResult();
assertThat(uploadResult.getKey(), is(assumedSourceKey));
final String assumedDestinationKey = UUID.randomUUID().toString();
final Copy copy =
transferManager.copy(sourceBucket.getName(), assumedSourceKey, targetBucket.getName(),
assumedDestinationKey);
final CopyResult copyResult = copy.waitForCopyResult();
assertThat(copyResult.getDestinationKey(), is(assumedDestinationKey));
final S3Object copiedObject = s3Client.getObject(targetBucket.getName(), assumedDestinationKey);
assertThat("Hashes for source and target S3Object do not match.",
HashUtil.getDigest(copiedObject.getObjectContent()) + "-1",
is(uploadResult.getETag()));
}
示例9: nuevoAnuncio
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
@RequestMapping("/anuncio/nuevo")
public String nuevoAnuncio(Model model,
@RequestParam String nombre,
@RequestParam String asunto,
@RequestParam String comentario,
@RequestParam String filename,
@RequestParam MultipartFile file) {
if (!file.isEmpty()) {
try {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(file.getContentType());
TransferManager transferManager = new TransferManager(s3);
transferManager.upload(bucket, filename, file.getInputStream(), objectMetadata);
} catch (Exception e) {
model.addAttribute("message", "You failed to upload " + filename + " => " + e.getMessage());
return "error";
}
} else {
model.addAttribute("message", "You failed to upload " + filename + " because the file was empty.");
return "error";
}
Anuncio anuncio = new Anuncio(nombre, asunto, comentario);
anuncio.setFoto(s3.getUrl(bucket, filename));
repository.save(anuncio);
return "anuncio_guardado";
}
示例10: store
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
@Override
public void store(File file) throws Exception {
LogUtils.debug(LOG_TAG, "Uploading new file. Name: " + file.getName());
TransferManager tm = new TransferManager(new DefaultAWSCredentialsProviderChain());
// TransferManager processes all transfers asynchronously,
// so this call will return immediately.
Upload upload = tm.upload(bucketName, file.getName(), file);
upload.waitForCompletion();
LogUtils.debug(LOG_TAG, "Successfully uploaded file to bucket.\nName: " + file.getName() + "\nBucket name: " +
bucketName);
tm.shutdownNow();
}
示例11: transferFileToS3
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
private void transferFileToS3(final String key) {
final long fileSizeMb = file.length() / (1024 * 1024);
getLogger().info("Uploading {} MB from file {} to {}", fileSizeMb, file, getS3Url());
final TransferManager transferManager = createTransferManager();
final Instant start = Instant.now();
final Upload upload = transferManager.upload(config.getDeploymentBucket(), key, file);
try {
upload.waitForCompletion();
getLogger().info("Uploaded {} to {} in {}", file, getS3Url(), Duration.between(start, Instant.now()));
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new AssertionError("Upload interrupted", e);
}
}
示例12: uploadFile
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
public static void uploadFile(String file_path, String bucket_name,
String key_prefix, boolean pause)
{
System.out.println("file: " + file_path +
(pause ? " (pause)" : ""));
String key_name = null;
if (key_prefix != null) {
key_name = key_prefix + '/' + file_path;
} else {
key_name = file_path;
}
File f = new File(file_path);
TransferManager xfer_mgr = new TransferManager();
try {
Upload xfer = xfer_mgr.upload(bucket_name, key_name, f);
// loop with Transfer.isDone()
XferMgrProgress.showTransferProgress(xfer);
// or block with Transfer.waitForCompletion()
XferMgrProgress.waitForCompletion(xfer);
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
xfer_mgr.shutdownNow();
}
示例13: uploadFileAsync
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
@Override
public Upload uploadFileAsync(final String bucketName, final String fileName, final File fileObj,
final CannedAccessControlList cannedAcl) throws AmazonClientException, AmazonServiceException, IOException {
LOGGER.info("uploadObjectAsync invoked, bucketName: {} , fileName: {} and cannedAccessControlList: {}", bucketName, fileName, cannedAcl);
final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, fileObj).withCannedAcl(cannedAcl);
final TransferManager transferMgr = new TransferManager(s3client);
return transferMgr.upload(putObjectRequest);
}
示例14: store
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
@Override
public F.Promise<Void> store(Path path, String key, String name) {
Promise<Void> promise = Futures.promise();
TransferManager transferManager = new TransferManager(credentials);
try {
Upload upload = transferManager.upload(bucketName, key, path.toFile());
upload.addProgressListener((ProgressListener) progressEvent -> {
if (progressEvent.getEventType().isTransferEvent()) {
if (progressEvent.getEventType().equals(ProgressEventType.TRANSFER_COMPLETED_EVENT)) {
transferManager.shutdownNow();
promise.success(null);
} else if (progressEvent.getEventType().equals(ProgressEventType.TRANSFER_FAILED_EVENT)) {
transferManager.shutdownNow();
logger.error(progressEvent.toString());
promise.failure(new Exception(progressEvent.toString()));
}
}
});
} catch (AmazonServiceException ase) {
logAmazonServiceException (ase);
} catch (AmazonClientException ace) {
logAmazonClientException(ace);
}
return F.Promise.wrap(promise.future());
}
示例15: uploadFileToS3
import com.amazonaws.services.s3.transfer.TransferManager; //导入方法依赖的package包/类
/**
* Uploads a file to S3 and returns the s3 file key. The bucket that is used is configured the properties file via
* s3.bucket
*
* @param s3Bucket the s3 bucket name
* @param localFile the local file to be uploaded
* @param s3FileKey the s3 file key that should be used
* @return a 2-element array, where element 0 is the s3 bucket and element 1 is the s3 file key
*/
public String[] uploadFileToS3(String s3Bucket, final Path localFile, final String s3FileKey)
throws IOException, InterruptedException {
if (localFile == null) {
throw new NullPointerException("localFile was null.");
}
if (isEmpty(s3FileKey)) {
throw new NullPointerException("objectFileKey cannot be null");
}
if (logger.isTraceEnabled()) {
logger.trace(format("uploadFileToS3(%s)", localFile.getFileName().toString()));
}
AWSCredentials awsCredentials = AmazonAWSHelper.getCredentials();
TransferManager tx = new TransferManager(awsCredentials);
ObjectMetadata metadata = new ObjectMetadata();
final String contentType = detectContentTypeFromFilename(s3FileKey);
if (logger.isDebugEnabled()) {
logger.debug(format("Setting contentType to '%s' in metadata for S3 object '%s'", contentType, s3FileKey));
}
metadata.setContentType(contentType);
Upload myUpload = tx.upload(s3Bucket, s3FileKey, Files.newInputStream(localFile), metadata);
myUpload.waitForCompletion();
String[] retval = {s3Bucket, s3FileKey};
if (logger.isDebugEnabled()) {
logger.debug(format("Upload to S3 was successful. bucket: '%s', file key: '%s'", s3Bucket, s3FileKey));
}
return retval;
}