当前位置:网站首页>21. Aliyun oss
21. Aliyun oss
2022-08-11 01:11:00 【Endless character】
目录
一、阿里云oss简介
1 - 入口
oss入口:控制台 -> 对象存储 oss
api文档:右下角 -> 常用入口 -> API 文档 -> The Document Center opens
2 - Resource terms
3 - bucket新建
- 其他的配置默认即可:If necessary, check the information by yourself
- HDFS服务:未开通
- 同城冗余存储:未开通
- 版本控制:未开通
- 服务端加密:无
- 实时日志查询:未开通
- 定时备份:未开通
二、The code controls the file upload
1 - 官方入门
- 官方快速入门
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func handleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// yourEndpoint填写Bucket对应的Endpoint,以华东1(杭州)为例,填写为https://oss-cn-hangzhou.aliyuncs.com.其它Region请按实际情况填写.
endpoint := "yourEndpoint"
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高.强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户.
accessKeyId := "yourAccessKeyId"
accessKeySecret := "yourAccessKeySecret"
// yourBucketNameFill in the storage space name.
bucketName := "yourBucketName"
// yourObjectName填写Object完整路径,The full path does not containBucket名称.
objectName := "yourObjectName"
// yourLocalFileName填写本地文件的完整路径.
localFileName := "yourLocalFileName"
// 创建OSSClient实例.
client, err := oss.New(endpoint, accessKeyId, accessKeySecret)
if err != nil {
handleError(err)
}
// 获取存储空间.
bucket, err := client.Bucket(bucketName)
if err != nil {
handleError(err)
}
// 上传文件.
err = bucket.PutObjectFromFile(objectName, localFileName)
if err != nil {
handleError(err)
}
}
- 查看Endpoint
2 - 创建Accesskey
- Accesskey入口: 控制台 -> 头像
3 - RAM管理
- RAMAccess control entry
- RAM新建用户
4 - 测试上传图片
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func handleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
}
func main() {
// Endpoint以杭州为例,其它Region请按实际情况填写.
endpoint := "http://oss-cn-shanghai.aliyuncs.com"
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高.强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号.
accessKeyId := "上一步获取的accessKeyId"
accessKeySecret := "上一步获取的accessKeySecret"
bucketName := "my-jp-oss"
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg.
objectName := "goods/first.jpg"
// <yourLocalFileName>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt.
localFileName := `D:\Downloads\360Downloads\lua.png`
// 创建OSSClient实例.
client, err := oss.New(endpoint, accessKeyId, accessKeySecret)
if err != nil {
handleError(err)
}
// 获取存储空间.
bucket, err := client.Bucket(bucketName)
if err != nil {
handleError(err)
}
// 上传文件.
err = bucket.PutObjectFromFile(objectName, localFileName)
if err != nil {
handleError(err)
}
}
三、前端直传oss
1 - Disadvantages of traditional file storage
2 - 前端直传oss的流程
考虑到安全问题:We shouldn't be directid和secrectSave it in the front-end direct transmissionoss
3 - ginIntegrated front-end direct transmission
- 官方地址:https://help.aliyun.com/document_detail/91818.html
- 按步骤实现
- 步骤1: Download the application server source code(Go版本)to the application server directory
- 步骤2: Download the client source code to PClocal directory on the side
- 步骤3: 修改CORS(According to the official steps)
- Unzip the downloaded files above into the test project
- appserver.go:修改id,secret以及bucketname
- Open Run in Terminal
- 浏览器访问:http://127.0.0.1:8082/
- 运行index.html
- 打开upload.js文件:修改serverUrl
if (xmlhttp!=null)
{
// serverUrl是 用户获取 '签名和Policy' 等信息的应用服务器的URL,请将下面的IP和Port配置为您自己的真实信息.
serverUrl = 'http://127.0.0.1:8082'
xmlhttp.open( "GET", serverUrl, false );
xmlhttp.send( null );
return xmlhttp.responseText
}
- Return to the front-end page and select File Upload
- Uploads that do not apply callbacks:aliyun-oss-appserver-js-master/upload.js,注释掉callback,Revisit after refreshing
new_multipart_params = {
'key' : g_object_name,
'policy': policyBase64,
'OSSAccessKeyId': accessid,
'success_action_status' : '200', //让服务端返回200,不然,默认会返回204
// 'callback' : callbackbody,
'signature': signature,
};
4 - 内网穿透
为什么需要内网穿透:续断 -> https://cloud.zhexi.tech/
aliyun-oss-appserver-js-master/upload.js:需要开启callback
new_multipart_params = {
'key' : g_object_name,
'policy': policyBase64,
'OSSAccessKeyId': accessid,
'success_action_status' : '200', //让服务端返回200,不然,默认会返回204
'callback' : callbackbody,
'signature': signature,
};
四、gin集成oss
The main modifications are listed below,Others can view the source code
- oss_web/static/js/upload.js:需要根据自己的ip修改serverUrl
- oss_web/utils/oss.go:
var expire_time int64 = 3000
,It is also possible to extract this configuration to nacos中
- nacos主要配置
{
"name":"oss_web",
"host":"192.168.78.1",
"tags":["mxshop","imooc","bobby","oss","web"],
"port":8029,
"oss":{
"key":"xxxxxxx",
"secrect":"xxxxx",
"host":"my-jp-oss.oss-cn-shanghai.aliyuncs.com",
"callback_url":"sadaxxxxxxx",
"upload_dir":"mxshop_images"
},
"jwt":{
"key":"VYLDYq3&hGWjWqF$K1ih"
},
"consul":{
"host":"192.168.78.31",
"port": 8500
}
}
六、完整源码
- 完整源码下载:mxshop_srvsV8.5.rar
- 源码说明:(nacos的ip配置自行修改,全局变量DEV_CONFIG设置:1=zsz,2=comp,3=home)
- goods_srv/model/sql/mxshop_goods.sql:包含了建表语句
- other_import/api.json:YApi的导入文件
- other_import/nacos_config_export_user.zip:nacos的user配置集导入文件
- other_import/nacos_config_export_goods.zip:nacos的goods配置集导入文件
边栏推荐
猜你喜欢
深度解析volatile关键字(保证够全面)
使用mysql语句操作数据表(table)
MSTP - Multiple Spanning Tree (Case + Configuration)
HW-蓝队工作流程(1)
【服务器数据恢复】raid5崩溃导致lvm信息和VXFS文件系统损坏的数据恢复案例
Apache Commons Configuration Remote Code Execution Vulnerability (CVE-2022-33980) Analysis & Reproduction
双机热备综合实验(VRRP+OSPF+VTP+NAT+DHCP+PVSTP+单臂路由)
还在用 Xshell?你 out 了,推荐一个更现代的终端连接工具,好用到爆!
BEVDepth: Acquisition of Reliable Depth for Multi-view 3D Object Detection 论文笔记
Some Experiences of Embedded Software Logging
随机推荐
池化技术有多牛?来,告诉你阿里的Druid为啥如此牛逼!
BEVDepth: Acquisition of Reliable Depth for Multi-view 3D Object Detection 论文笔记
[GXYCTF2019]BabySQli
如何破坏Excel文件,让其显示文件已损坏方法
Use mysql statement to operate data table (table)
21、阿里云oss
dump_stack()
Ambari迁移Spark2到其它机器(图文教程)
The statistical data analysis, interview manual"
详谈二叉搜索树
② 关系数据库标准语言SQL 数据定义(创建、修改基本表)、数据更新(增删改)
编程技巧│selenium 更新 chromedriver 驱动
Kunpeng compilation and debugging and basic knowledge of native development tools
#yyds Dry Goods Inventory#[Yugong Series] August 2022 Go Teaching Course 008-Integer of Data Types
什么是“门”电路(电子硬件)
Jvm. Profiling tools (jconsole, jvisualvm, arthas, jprofiler, mat)
Shell Text Three Musketeers Sed
BEVDepth: Acquisition of Reliable Depth for Multi-view 3D Object Detection Paper Notes
微信小程序通过URL Scheme动态的渲染数据
【视频】报告分享|2021年保险行业数字化洞察