1. 程式人生 > >使用Google雲端儲存上傳圖片

使用Google雲端儲存上傳圖片

谷歌雲端儲存官網:https://console.cloud.google.com/

1.獲取服務賬號金鑰:官網--左側導航API和服務--憑據--建立服務賬號金鑰

2.將下載的Json檔案配置到環境變數(配置完成後重啟電腦)

A.Windows:

set GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>

set GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
set GOOGLE_APPLICATION_CREDENTIALS
=<path_to_service_account_file>

set GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
set GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>

B.Linux:
set GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>

set GOOGLE_APPLICATION_CREDENTIALS=
<path_to_service_account_file>

export GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>

3.程式碼


import com.google.cloud.storage.Acl;
import com.google.cloud.storage.Acl.Role;
import com.google.cloud.storage.Acl.User;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;


import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
/**
 * 
 * @author lin
 *
 */
public class GoogleUpdateFileUtil {
 
// 儲存空間
private final static String bucketName = "btc666com_001";

//服務
private static Storage storage = null;
 
//圖片的儲存路徑
private static String imgUrl = null;
 
 static {
    storage = StorageOptions.getDefaultInstance().getService();
 }
 
 
/**
* 上傳圖片到Google雲端儲存
* @param file : 檔案
* @param prefix : 檔名字首 
* @return 
* @throws IOException
*/
@SuppressWarnings("deprecation")
public static String uploadFile(MultipartFile file,String prefix) throws IOException   {
    //檔名  
   DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYYMMddHHmmss");
   DateTime dt = DateTime.now(DateTimeZone.UTC);
   String dtString = dt.toString(dtf);
   final String fileName =prefix + dtString+file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."),file.getOriginalFilename().length());
        System.err.println("fileName:" + fileName);
  
       //上傳檔案 
   BlobInfo blobInfo =
       storage.create(
           BlobInfo
               .newBuilder(bucketName, fileName)
               // Modify access list to allow all users with link to read file
               .setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))  
               .setContentType("image/jpeg")
               .build(),
           file.getInputStream());
     
     //System.out.println("下載連線:"+blobInfo.getMediaLink());
   imgUrl = "https://storage.googleapis.com/" + bucketName + "/" + fileName;
 
   return imgUrl;
   
 }
 
}

4.maven座標:

    <!-- 谷歌雲端儲存 -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.6.0</version>
</dependency>
     <!-- 谷歌雲端儲存 --> 

set GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>