[JAVA] [Note] Download from S3, upload to S3

  1. File download from S3 This time, I described the case of downloading all the files under the target path of the bucket.
  AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion("Region name").build();

  //Get the list of files under the bucket on S3
  ObjectListing objListing = s3Client.listObjects("Bucket name");
  List<S3ObjectSummary> objList = objListing.getObjectSummaries();

  try {
    //Process files on S3
    for (S3ObjectSummary obj : objList) {
      //Since objList contains all folders and file information under the bucket, it is necessary to narrow down by the target path.
      //Do not download if the target path is not included or the size is 0
      if (!StringUtils.contains(obj.getKey(), "Target path") || obj.getSize() == 0) {
        continue;
      }

      //Download process below
      // obj.getKey()Is"Target path/file name"Has become
      GetObjectRequest request = new GetObjectRequest("Bucket name", obj.getKey());
      //Only the file name
      String fileName = obj.getKey().replace("Target path", "");
      //Generate download destination file
      File file = new File(fileName);
      if (s3Client.getObject(request, file) == null) {
        //Download failure
      }
    }
  } catch (IOException e) {
    throw e;
  }

  1. Upload to S3 When uploading a file to the target path under the bucket
  try {
    AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion("Region name").build();

    File file = new File("File name to be uploaded");
    PutObjectRequest request = new PutObjectRequest("Bucket name", "Target path" + file.getName(), file);
    request.setCannedAcl(CannedAccessControlList.PublicRead);
    s3Client.putObject(request);
  } catch (Exception e) {
    throw e;
  }

Recommended Posts

[Note] Download from S3, upload to S3
How to download images from AWS S3 (rails, carrierwave)
upload images to refile heroku S3
Upload Rails app image file to S3
[Java] Test S3 upload / download using "S3 ninja"
Java upload and download notes to Azure storage
Upload and download notes in java on S3
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Migrate from JUnit 4 to JUnit 5
From Java to Ruby !!
Can't upload to heroku
My 1880's ~ Fixed time zone from LMT to JST ~
[Note] Flow from docker installation to JupyterLab startup on ubuntu
[Rails] Create API to download files with Active Storage [S3]
Try Spring Boot from 0 to 100.
Migration from Cobol to JAVA
Switch from slim3-gen to slim3-gen-jsr269
Moving from AWS to PaizaCloud
Upload / download / bulk delete data to S3 using Amazon S3 Client Builder with AWS SDK for Java
New features from Java7 to Java8
Migrating from vargrant to docker
Connect from Java to PostgreSQL
S3 Presigner added to aws-sdk-java-v2
[Note] How to write Dockerfile/docker-compose.yml
Convert from ○ months to ○ years ○ months
Rewriting from applet to application
Save twitter icon to s3
Change from SQLite3 to PostgreSQL
From Ineffective Java to Effective Java
How to migrate from JUnit4 to JUnit5
Upload files from CentOS to Azure Data Lake Gen2 with azcopy
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws
[Rails] How to upload images to AWS S3 using refile and refile-s3