当前位置:网站首页>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配置集导入文件
边栏推荐
- Jvm. Profiling tools (jconsole, jvisualvm, arthas, jprofiler, mat)
- 成功解决raise TypeError(‘Unexpected feature_names type‘)TypeError: Unexpected feature_names type
- 详解JDBC的实现与优化(万字详解)
- postgresql参数意义
- 深度解析volatile关键字(保证够全面)
- url转成obj或者obj转成url的方法
- 编程技巧│selenium 更新 chromedriver 驱动
- 【pypdf2】合并PDF、旋转、缩放、裁剪、加密解密、添加水印
- Mysql database installation and configuration detailed tutorial
- 【服务器数据恢复】raid5崩溃导致lvm信息和VXFS文件系统损坏的数据恢复案例
猜你喜欢
postgresql parameter meaning
嵌入式软件打log的一些心得
Update chromedriver driver programming skills │ selenium
Dual machine thermal for comprehensive experiment (VRRP + OSPF + + NAT + DHCP + VTP PVSTP + single-arm routing)
Kunpeng compilation and debugging and basic knowledge of native development tools
leetcode 前K个高频单词
20张图,全面掌握MVCC原理!
时间戳转换为日期格式、获取当前时间戳
③ 关系数据库标准语言SQL 数据查询(SELECT)
构建资源的弹性伸缩
随机推荐
Shell 文本三剑客 Sed
[GXYCTF2019]BabySQli
J9数字论:DAO治理更像一种生态过程:治理原生于网络,不断演变
C# using timer
力扣------值相等的最小索引
什么是“门”电路(电子硬件)
16. Sum of the nearest three numbers
二维数组实战项目--------《扫雷游戏》
成功解决TypeError: can‘t multiply sequence by non-int of type ‘float‘
Still using Xshell?You are out, recommend a more modern terminal connection tool, easy to use!
How to build speed, speed up again
双机热备综合实验(VRRP+OSPF+VTP+NAT+DHCP+PVSTP+单臂路由)
数据分析面试手册《SQL篇》
C# JObject解析JSON数据
How to determine the size of the version number
Data Analysis Interview Manual "SQL"
C # - delegate detailed usage
networkmanager无法打开
复制带随机指针的链表——LeetCode
WinForm(五)控件和它的成员