简体   繁体   English

如何使用 java 在 amazon s3 中存储文件

[英]How to store a file in amazon s3 using java

im new in s3 and i need some help please/我是 s3 的新手,我需要一些帮助/

1.I get some file from rest api its looks like 1.我从 rest api 得到一些文件,它看起来像

@PostMapping ("/aaa")
  public void aaa(@RequestParam("file")MultipartFile file)

how do i can put it to s3 without saving in local like this我怎么能把它放到s3而不像这样保存在本地

File f=new File(System.getProperty("java.io.tmpdir")+"/"+file.getOriginalFilename());
    file.transferTo(f);
    s3client.putObject("netapi", file.getOriginalFilename(),f);

i need to put file like it is - for example i put img.png to rest api and i need to get link from s3 to same file that be able to download and open without any converting on client pc我需要按原样放置文件-例如,我将 img.png 放入 rest api 并且我需要从 s3 获取链接到能够下载和打开而无需在客户端 pc 上进行任何转换的同一文件

2.second question is there any way to do not use MultipartFile in rest api - may be there is a way to covert inputStream that i can get from HttpServletRequest.getInputStream() to a file with same file name and file extension 2.第二个问题是有什么方法可以不在 rest api 中使用 MultipartFile - 可能有一种方法可以将我可以从 HttpServletRequest.getInputStream() 获取的 inputStream 转换为具有相同文件名和文件扩展名的文件

i use sdk for java 1.X我将 sdk 用于 java 1.X

implementation(group = "com.amazonaws",name="aws-java-sdk-s3",version = "1.11.1008")

To answer your question about working with a Spring Controller, you can get the byte array that represents the file like this:要回答有关使用 Spring Controller 的问题,您可以获得代表文件的字节数组,如下所示:

//Upload an image to send to a S3 bucket
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public ModelAndView singleFileUpload(@RequestParam("file") MultipartFile file) {

        try {

            // Now i can add this to an S3 Bucket
            byte[] bytes = file.getBytes();
            String name =  file.getOriginalFilename() ;

           // Put the file into the bucket
            s3Client.putObject(bytes, "mybucket", name);
       
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ModelAndView(new RedirectView("photo"));
    }

YOu can use that byte array and file name to put the file into an Amazon S3 bucket without saving it as a local file.您可以使用该字节数组和文件名将文件放入 Amazon S3 存储桶,而无需将其保存为本地文件。

To put this into an AMazon S3 bucket, you can use this code:要将其放入亚马逊 S3 存储桶,您可以使用以下代码:

     // Places the file into a S3 bucket
     public String putObject(byte[] data, String bucketName, String objectKey) {
    
            s3 = getClient();
    
            try {
                //Put a file into the bucket
                PutObjectResponse response = s3.putObject(PutObjectRequest.builder()
                                .bucket(bucketName)
                                .key(objectKey)
                                .build(),
                        RequestBody.fromBytes(data));
    
                return response.eTag();
    
            } catch (S3Exception e) {
                System.err.println(e.getMessage());
                System.exit(1);
            }
            return "";
        }

 private S3Client getClient() {
        // Create the S3Client object
        Region region = Region.US_WEST_2;
        S3Client s3 = S3Client.builder()
                .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                .region(region)
                .build();

        return s3;
    }

This code shows you how to handle a file that is posted to a Spring BOOT controller, how to get the file name and byte array, and how to put the file into an Amazon S3 bucket using V2 S3 code.此代码向您展示如何处理发布到 Spring BOOT controller 的文件,如何获取文件名和字节数组,以及如何使用 V2 S3 代码将文件放入 Amazon S3 存储桶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM