Pu Zhibing
6 天以前 4c99ee7028c3fe58a2cd4b8273b22c75c45574fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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");
    }
    
}