当前位置: 首页 > news >正文

Java实现Google cloud storage 文件上传,Google oss

storage 控制台位置
在这里插入图片描述创建一个bucket

在这里插入图片描述

点进bucket里面,权限配置里,公开访问,在互联网上公开,需要配置角色权限
在这里插入图片描述
新增一个访问权限 ,账号这里可以模糊搜索,
在这里插入图片描述
角色配置
在这里插入图片描述
在这里插入图片描述
给allUser配置俩角色就可以出现 在互联网上公开.

配置完点保存就可以了。
在这里插入图片描述

下载json文件,找到服务账号

在这里插入图片描述

账号
在这里插入图片描述
选择角色
在这里插入图片描述
下载json文件

在这里插入图片描述

上传方法

package com.jeesea.manage.utils;import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.io.InputStream;public class GoogleStorageUtils {// 存储桶名称static String bucketName = "mhxq";public static String upload(MultipartFile file , String objectName,String contentType){try {//json文件放到了 resources 下面   // 从resources目录中加载JSON密钥文件的输入流并确保资源的正确关闭try (InputStream inputStream = new ClassPathResource("key.json").getInputStream()) {// 初始化Google Cloud Storage服务Storage storage = StorageOptions.newBuilder().setCredentials(GoogleCredentials.fromStream(inputStream)).build().getService();// 上传文件BlobId blobId = BlobId.of(bucketName, objectName);BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();Blob blob = storage.create(blobInfo, file.getBytes());//修改已经上传的文件类型为 image/jpgblob.toBuilder().setContentType(contentType).build().update();// 返回文件访问链接return "https://storage.googleapis.com/"+bucketName +"/"+ objectName ;}} catch (IOException e) {e.printStackTrace();return "上传文件失败";}}/*** 上传图片到 Google Cloud storage* @param file* @param objectName* @return*/public static String uploadImg(MultipartFile file , String objectName){return  upload(file , objectName , "image/jpg");}/*** 上传mp4视频到 Google Cloud storage* @param file* @param objectName* @return*/public static String uploadVideo(MultipartFile file , String objectName){return  upload(file , objectName , "video/mp4");}
}
http://www.lryc.cn/news/113473.html

相关文章:

  • 适配器模式(AdapterPattern)
  • Apache Kafka Learning
  • 手把手教你用idea实现Java连接MySQL数据库
  • Ubuntu 22.04安装和使用ROS1可行吗
  • 83 | Python可视化篇 —— Bokeh数据可视化
  • 图像 检测 - RetinaNet: Focal Loss for Dense Object Detection (arXiv 2018)
  • MySQL 与MongoDB区别
  • Kaggle First Place Winner Solution Study——多变量回归问题
  • 分布式应用:Zookeeper 集群与kafka 集群部署
  • Last-Mile Embodied Visual Navigation 论文阅读
  • thunder gbm
  • 数据结构--单链表
  • 过程:从虚拟机上添加 git 并成功提交到 GitLab 的全过程
  • 机器学习笔记之优化算法(九)收敛速度的简单认识
  • FPGA学习——Altera IP核调用之PLL篇
  • 经纬度坐标工具
  • 如何使用伪元素::before和::after?
  • Visual Studio Code中对打开的脚本格式统一
  • 补充JDK源码-IDEA集成工具
  • Git Submodule 更新子库失败 fatal: Unable to fetch in submodule path
  • Springboot切面打印日志
  • ubuntu上回环设备/dev/loop0占用100%清理
  • List list=new ArrayList()抛出的ArrayIndexOutOfBoundsException异常
  • 桶排序算法
  • P8604 [蓝桥杯 2013 国 C] 危险系数
  • Excel·VBA表格横向、纵向相互转换
  • Leetcode-每日一题【剑指 Offer 06. 从尾到头打印链表】
  • LeetCode--HOT100题(22)
  • 产品体系架构202308版
  • Linux systemctl 简单介绍与使用