无关风月
2024-09-06 6a90f2d1feaf583c211d5f9fe95dff7f7c748ed3
UserIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GoogleCloudStorageUtil.java
New file
@@ -0,0 +1,82 @@
package com.stylefeng.guns.modular.system.util;
import com.google.cloud.storage.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/**
 * google对象存储
 * @author zhibing.pu
 * @Date 2024/8/31 9:18
 */
public class GoogleCloudStorageUtil {
   public static String upload(MultipartFile file){
      // The ID of your GCP project
      // String projectId = "your-project-id";
      // The ID of your GCS bucket
      // String bucketName = "your-unique-bucket-name";
      // The ID of your GCS object
      // String objectName = "your-object-name";
      // The path to your file to upload
      // String filePath = "path/to/your/file"
      String fileName = file.getOriginalFilename();
      String projectId = "i-go-gcp";
      String bucketName = "i-go";
      Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
      BlobId blobId = BlobId.of(bucketName, fileName);
      BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
      // Optional: set a generation-match precondition to avoid potential race
      // conditions and data corruptions. The request returns a 412 error if the
      // preconditions are not met.
      Storage.BlobWriteOption precondition;
      if (storage.get(bucketName, fileName) == null) {
         // For a target object that does not yet exist, set the DoesNotExist precondition.
         // This will cause the request to fail if the object is created before the request runs.
         precondition = Storage.BlobWriteOption.doesNotExist();
      } else {
         // If the destination already exists in your bucket, instead set a generation-match
         // precondition. This will cause the request to fail if the existing object's generation
         // changes before the request runs.
         precondition =
               Storage.BlobWriteOption.generationMatch(
                     storage.get(bucketName, fileName).getGeneration());
      }
      try {
         storage.createFrom(blobInfo, file.getInputStream(), precondition);
         System.out.println(
               "File uploaded to bucket " + bucketName + " as " + fileName);
         makeObjectPublic(projectId, bucketName, fileName);
         return "https://storage.googleapis.com/i-go/" + fileName;
      } catch (IOException e) {
         throw new RuntimeException(e);
      }
   }
   /**
    * 设置对象公开访问
    * @param projectId
    * @param bucketName
    * @param objectName
    */
   public static void makeObjectPublic(String projectId, String bucketName, String objectName) {
      // String projectId = "your-project-id";
      // String bucketName = "your-bucket-name";
      // String objectName = "your-object-name";
      Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
      BlobId blobId = BlobId.of(bucketName, objectName);
      storage.createAcl(blobId, Acl.of(Acl.User.ofAllUsers(), Acl.Role.READER));
      System.out.println(
            "Object " + objectName + " in bucket " + bucketName + " was made publicly readable");
   }
}