From 6a90f2d1feaf583c211d5f9fe95dff7f7c748ed3 Mon Sep 17 00:00:00 2001 From: 无关风月 <443237572@qq.com> Date: 星期五, 06 九月 2024 17:42:20 +0800 Subject: [PATCH] Merge branch '2.0' of http://120.76.84.145:10101/gitblit/r/java/IgoTravel into 2.0 --- UserIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GoogleCloudStorageUtil.java | 82 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 82 insertions(+), 0 deletions(-) diff --git a/UserIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GoogleCloudStorageUtil.java b/UserIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GoogleCloudStorageUtil.java new file mode 100644 index 0000000..6a91d7c --- /dev/null +++ b/UserIGOTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GoogleCloudStorageUtil.java @@ -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"); + } + +} -- Gitblit v1.7.1