如果这是对另一个问题的重申,请提前道歉,尽管我花了一天的时间进行研究和实验,但未成功:
我正在用Java编写即时消息应用程序,借此将客户端的个人资料图像存储在AWS的S3存储桶中。 我已经成功存储并检索了我的person comp上的文件。导出项目后(使用嵌入了外部JARS的Eclipse可执行JAR文件),以编程方式使用Java S3 JDK进行编程,但甚至无法通过任何其他机器成功连接到我的AWS S3(使用凭证和区域)
以下是简化的测试代码:
private void initiateS3Connection() {
AmazonS3 s3Client = null;
try {
JOptionPane.showMessageDialog(null, "Initiating Application");
BasicAWSCredentials awsCreds = new BasicAWSCredentials("<access-key>",
"<secret-access-key>"); //REAL keys placed in code properly, works when run through personal comp.
JOptionPane.showMessageDialog(null, "Created basic aws credentials");
Regions region = Regions.fromName("us-east-1");
JOptionPane.showMessageDialog(null, "Created region");
s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withRegion(region).build();
// Currently, program stops here indefinitely via JAR exportation, no exceptions
// thrown
JOptionPane.showMessageDialog(null, "Created AmazonS3Client: " + s3Client);
File s3Object = getProfileImage("4profileimagebucket", "4_profile_image", s3Client);
JOptionPane.showMessageDialog(null, s3Object);
Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler " + "s3imagetest.jpg");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
public static File getProfileImage(String bucket_name, String key_name, AmazonS3 s3Client) throws IOException {
File profile_image_file = new File("s3imagetest.jpg");
S3Object s3Object = s3Client.getObject(new GetObjectRequest(bucket_name, key_name));
InputStream inputStream = s3Object.getObjectContent();
byte[] contentBuffer = new byte[1024];
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(profile_image_file));
int bytesRead = 0;
while ((bytesRead = inputStream.read(contentBuffer)) != -1) {
outputStream.write(contentBuffer, 0, bytesRead);
}
return profile_image_file;
}
}
此代码可以通过Eclipse在我的个人计算机上完美执行,甚至可以在我的S3存储桶中加载和打开图像。
当对可运行的JAR执行并在另一台计算机上运行时,该程序将在显示表明已创建区域的JOptionPane之后立即停止。不会引发任何异常,因为catch子句中的JOptionPane不执行,似乎程序正在无限期地尝试执行“ AmazonS3ClientBuilder”
关于使用Java AWS JDK进行远程S3连接是否缺少/不了解?
很明显,在这种情况下,不会从指定的存储桶中加载文件,但是如果有人想指出与此相关的问题,我也会保留“ getProfileImage”方法的代码。
感谢您的光临!