gpt4 book ai didi

java - 通过 html 文件字段将图像文件上传到我的 aws s3 存储桶

转载 作者:行者123 更新时间:2023-12-01 11:45:07 25 4
gpt4 key购买 nike

两天以来我一直在谷歌上寻找解决方案,但没有任何解决方案。

DefaultAWSCredentialsProviderChain credentialProviderChain = new DefaultAWSCredentialsProviderChain();
TransferManager tx = new TransferManager(credentialProviderChain.getCredentials());
Upload myUpload = tx.upload(existingBucketName, keyName, new File("C:/Users/Desktop/Management_System.jpg"));
// While the transfer is processing, you can work with the transfer object
while (myUpload.isDone() == false) {
System.out.println(myUpload.getProgress().getPercentTransferred() + "%");
}
myUpload.waitForCompletion();
tx.shutdownNow();

此代码工作正常,并上传图像 Management_System.jpg,该图像已成功放入我的桌面到 aws s3 存储桶。我有一个带有文件字段的 html 表单。我想浏览我的计算机中的任何图像并将其上传到我的 s3 存储桶中。我也尝试了下面的代码,当我在 amazon ec2 上测试它时,它在我的本地主机上运行良好,然后它抛出类似“ImageIO write Invalid native argument”的错误。我已将文件字段写入表单名称 [照片]。所以我的代码如下:

BufferedImage src = ImageIO.read(photo.getInputStream());
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(src, "JPG", os);
byte[] buffer = os.toByteArray();
InputStream is = new ByteArrayInputStream(buffer);

AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());

if(isValidFile(s3client, existingBucketName, info.getPhoto())){
s3client.deleteObject(new DeleteObjectRequest(existingBucketName, info.getPhoto()));
}

ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(buffer.length);
meta.setContentType(photo.getContentType());
s3client.putObject(new PutObjectRequest(existingBucketName, keyName, is, meta));
s3client.setObjectAcl(existingBucketName, keyName, CannedAccessControlList.PublicRead);

请指导我哪里做错了?我在 ec2 实例上使用 tomcat 7,我的本地主机版本也是 tomcat 7。我不明白这是版本兼容性问题还是其他问题...

更新答案:

终于我让它工作了。这是我通过 html 表单字段上传完整文件的代码。我将其发布在这里是为了帮助其他人,如果您觉得有帮助,请投票给我。谢谢

AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(DashboardController.class.getResourceAsStream("/AWSCredentials.properties")));
TransferManager tx = new TransferManager(new PropertiesCredentials(DashboardController.class.getResourceAsStream("/AWSCredentials.properties")));
//AmazonS3 s3 = new AmazonS3Client(new DefaultAWSCredentialsProviderChain());
if(isValidFile(s3, BucketName, info.getPhoto())){
s3.deleteObject(new DeleteObjectRequest(BucketName, info.getPhoto()));
}
S3Object s3Object = new S3Object();
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType(photo.getContentType());
meta.setContentLength(photo.getSize());
meta.setHeader("filename", NewkeyName);
ByteArrayInputStream bis = new ByteArrayInputStream(photo.getBytes());
s3Object.setObjectContent(bis);

//s3.putObject(new PutObjectRequest(BucketName, NewkeyName, bis, omd));
Upload myUpload = tx.upload(BucketName, NewkeyName, bis, meta);
while (myUpload.isDone() == false) {
System.out.println(myUpload.getProgress().getPercentTransferred() + "%");
}
if(myUpload.isDone() == true){
System.out.println("100%");
}
s3.setObjectAcl(BucketName, NewkeyName, CannedAccessControlList.PublicRead);
myUpload.waitForCompletion();
tx.shutdownNow();

s3Object.close();

最佳答案

问题似乎出现在 S3 代码之前。

错误消息与 ImageIO.write 相关。

稍微搜索一下就会发现 OpenJDK doesn't include JPEG support .

您可以在没有 S3 的情况下在本地测试这部分代码(确保使用相同的 JRE)。

你为什么要使用 ImageIO?在放入 S3 之前是否进行转码

关于java - 通过 html 文件字段将图像文件上传到我的 aws s3 存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29213667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com