![](/img/trans.png)
[英]Amazon SES reading emails stored in an s3 bucket using java from S3ObjectInputStream object
[英]Store Java object to Amazon S3 bucket
我有一個 Java 對象。 我得到這個作為我們系統底層 API 的響應。 我需要將此對象存儲在 S3 存儲桶內的文件夾中。
我發現了這個https://stackoverflow.com/a/41017427/9715520 。 但是,正如我所說,我需要將對象上傳到存儲桶內的文件夾中。 有沒有辦法我也可以指定文件夾名稱。 該示例講述了如何將對象保存到 S3 存儲桶。 如果一個存儲桶中有多個文件夾並且需要存儲在特定文件夾中怎么辦。 我們有什么辦法嗎?
我就是這樣做的。 下面的代碼是否足夠,或者我是否需要明確創建 s3 客戶端來上傳對象?
private void s3Upload(MyObject myObject) {
TransferManager transferManager = TransferManagerBuilder.standard().build();
ObjectMapper objectMapper = new ObjectMapper();
byte[] bytesToWrite = objectMapper.writeValueAsBytes(myObject);
ObjectMetadata omd = new ObjectMetadata();
omd.setContentLength(bytesToWrite.length);
String uniqueID = UUID.randomUUID().toString();
transferManager.upload(<bucketName>, uniqueID, new ByteArrayInputStream(bytesToWrite), omd);
}
要上傳對象(例如 PDF),建議您使用AWS SDK for Java V2 。 使用此 API,您有 2 個選擇。
1. 您可以使用S3Client對象將對象上傳到 Amazon S3 存儲桶。
2. 您可以使用TransferManager對象將對象上傳到 Amazon S3 存儲桶。
Amazon S3 控制台支持將文件夾概念作為對對象進行分組的一種方式。 例如,如果要將 PDF 文件存儲在名為folder2的文件夾中,請在objectKey中指定名稱。 例如:
字符串 objectKey = "folder2/myPDF.pdf"
當您運行此代碼時,myPDF 位於文件夾 2 中——如此屏幕截圖所示:
如果您不熟悉使用適用於 Java V2 的AWS 開發工具包,請參閱Java V2 開發指南了解您需要的所有概念。 例如,如何使用憑證。
S3客戶端
以下代碼向您展示了如何使用此對象上傳內容。
package com.example.s3;
// snippet-start:[s3.java2.s3_object_upload.import]
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
// snippet-end:[s3.java2.s3_object_upload.import]
/**
* Before running this Java V2 code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class PutObject {
public static void main(String[] args) {
final String usage = "\n" +
"Usage:\n" +
" <bucketName> <objectKey> <objectPath> \n\n" +
"Where:\n" +
" bucketName - The Amazon S3 bucket to upload an object into.\n" +
" objectKey - The object to upload (for example, book.pdf).\n" +
" objectPath - The path where the file is located (for example, C:/AWS/book2.pdf). \n\n" ;
if (args.length != 3) {
System.out.println(usage);
System.exit(1);
}
String bucketName =args[0];
String objectKey = args[1];
String objectPath = args[2];
System.out.println("Putting object " + objectKey +" into bucket "+bucketName);
System.out.println(" in bucket: " + bucketName);
ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder()
.region(region)
.credentialsProvider(credentialsProvider)
.build();
String result = putS3Object(s3, bucketName, objectKey, objectPath);
System.out.println("Tag information: "+result);
s3.close();
}
// snippet-start:[s3.java2.s3_object_upload.main]
public static String putS3Object(S3Client s3,
String bucketName,
String objectKey,
String objectPath) {
try {
Map<String, String> metadata = new HashMap<>();
metadata.put("x-amz-meta-myVal", "test");
PutObjectRequest putOb = PutObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
.metadata(metadata)
.build();
PutObjectResponse response = s3.putObject(putOb,
RequestBody.fromBytes(getObjectFile(objectPath)));
return response.eTag();
} catch (S3Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
// Return a byte array.
private static byte[] getObjectFile(String filePath) {
FileInputStream fileInputStream = null;
byte[] bytesArray = null;
try {
File file = new File(filePath);
bytesArray = new byte[(int) file.length()];
fileInputStream = new FileInputStream(file);
fileInputStream.read(bytesArray);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bytesArray;
}
// snippet-end:[s3.java2.s3_object_upload.main]
}
TransferManager 對象
以下代碼向您展示了如何使用此 Java 對象將內容上傳到 S3 存儲桶。
package com.example.transfermanager;
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.transfer.s3.FileUpload;
import software.amazon.awssdk.transfer.s3.S3TransferManager;
import java.nio.file.Paths;
/**
* Before running this Java V2 code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class UploadObject {
public static void main(String[] args) {
final String usage = "\n" +
"Usage:\n" +
" <bucketName> <objectKey> <objectPath> \n\n" +
"Where:\n" +
" bucketName - The Amazon S3 bucket to upload an object into.\n" +
" objectKey - The object to upload (for example, book.pdf).\n" +
" objectPath - The path where the file is located (for example, C:/AWS/book2.pdf). \n\n" ;
if (args.length != 3) {
System.out.println(usage);
System.exit(1);
}
long mb = 1024;
String bucketName = args[0];
String objectKey = args[1];
String objectPath = args[2];
System.out.println("Putting an object into bucket "+bucketName +" using the S3TransferManager");
ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
Region region = Region.US_EAST_1;
S3TransferManager transferManager = S3TransferManager.builder()
.s3ClientConfiguration(cfg ->cfg.region(region)
.credentialsProvider(credentialsProvider)
.targetThroughputInGbps(20.0)
.minimumPartSizeInBytes(10 * mb))
.build();
uploadObjectTM(transferManager, bucketName, objectKey, objectPath);
System.out.println("Object was successfully uploaded using the Transfer Manager.");
transferManager.close();
}
public static void uploadObjectTM( S3TransferManager transferManager, String bucketName, String objectKey, String objectPath) {
FileUpload upload =
transferManager.uploadFile(u -> u.source(Paths.get(objectPath))
.putObjectRequest(p -> p.bucket(bucketName).key(objectKey)));
upload.completionFuture().join();
}
}
另一個更新
看這行代碼:
PutObjectResponse 響應 = s3.putObject(putOb, RequestBody.fromBytes (getObjectFile(objectPath)));
只要您獲得要上傳到字節數組的數據,就可以使用此代碼將其上傳到 Amazon S3。 我以物理 PDF 文件為例。 但是,如果您從另一個來源獲得 byte[],它仍然可以工作。
RequestBody 也支持這些 Javadocs 中討論的其他方法。
https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/sync/RequestBody.html
希望這可以消除您的疑問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.