Pu Zhibing
5 小时以前 821fc2f632f8b974a2c0fc37630e13c5fbe6086c
ManagementIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GoogleCloudStorageUtil.java
@@ -1,5 +1,6 @@
package com.stylefeng.guns.modular.system.util;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.WriteChannel;
import com.google.cloud.storage.*;
import org.springframework.web.multipart.MultipartFile;
@@ -19,6 +20,10 @@
   
   
   public static String upload(MultipartFile file){
      // The ID of your GCP project
      // String projectId = "your-project-id";
      
@@ -33,7 +38,19 @@
      String fileName = file.getOriginalFilename();
      String projectId = "i-go-gcp";
      String bucketName = "i-go";
      Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
        GoogleCredentials defaultCredentials = null;
        try {
            defaultCredentials = GoogleCredentials.getApplicationDefault();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        defaultCredentials.createScoped("https://www.googleapis.com/auth/cloud-platform");
      Storage storage = StorageOptions
            .newBuilder()
            .setProjectId(projectId)
            .setCredentials(defaultCredentials)
            .build()
            .getService();
      BlobId blobId = BlobId.of(bucketName, fileName);
      BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();