当前位置:网站首页>aws s3 upload file
aws s3 upload file
2022-08-02 02:45:00 【Asimov__】
go get github.com/aws/aws-sdk-go
package mainimport ("bytes""fmt""github.com/aws/aws-sdk-go/aws""github.com/aws/aws-sdk-go/aws/credentials""github.com/aws/aws-sdk-go/aws/session""github.com/aws/aws-sdk-go/service/s3""io/ioutil""log""net/http""os")func Read(filepath string) []byte {f, err := os.Open(filepath)if err != nil {log.Println("read file fail", err)return nil}defer f.Close()fd, err := ioutil.ReadAll(f)if err != nil {log.Println("read to fd fail", err)return nil}return fd}func UploadFileToS3(s *session.Session, raw []byte, filename string) (string, error) {tempFileName := "cards/" + filename_, err := s3.New(s).PutObject(&s3.PutObjectInput{Bucket: aws.String("bucket name"), // bucket nameKey: aws.String(tempFileName),Body: bytes.NewReader(raw),ContentType: aws.String(http.DetectContentType(raw)),})if err != nil {log.Println("PUT err", err)return "", err}fileUrl := "https://bucketname.s3.ap-east-1.amazonaws.com/" + tempFileNamereturn fileUrl, err}func main() {s, err := session.NewSession(&aws.Config{Region: aws.String("ap-east-1"), // Replace the region of your own accountCredentials: credentials.NewStaticCredentials("AccessKeyID","SecretAccessKey","SessionToken"), // Sessiontoken is process related and should be returned in the connection (can be null)})if err != nil {log.Println("aws failed", err)}for i := 0; i < 10000; i++ {f1 := fmt.Sprintf("green_%d.gif", i)res := Read("./green/" + f1)fileName, err1 := UploadFileToS3(s, res, f1)if err1 != nil {//log.Println("Upload failed ", err1)} else {log.Println("upload success ", fileName)}}}AccessKeyID SecretAccessKey: Apply in the security_credentials of the account (or create a user in IAM)

边栏推荐
猜你喜欢

详解最强分布式锁工具:Redisson

使用DBeaver进行mysql数据备份与恢复

Oracle数据类型介绍

永磁同步电机36问(二)——机械量与电物理量如何转化?

Safety (2)

Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided

nacos startup error, the database has been configured, stand-alone startup

51. 数字排列

Remember a pit for gorm initialization

Analysis of the status quo of digital transformation of manufacturing enterprises
随机推荐
GTK RGB图像绘制
数仓:数仓从ETL到ELT架构的转化以及俩者的区别
JVM调优实战
线程的不同状态
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
字符串常用方法
The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
字典常用方法
EFCore 反向工程
Electronic Manufacturing Warehouse Barcode Management System Solution
2022牛客多校三_F G
最大层内元素和
Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
The state status is displayed incorrectly after the openGauss switch
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
789. 数的范围
因为WiFi原因navicat 无法连接数据库Mysql
2022 NPDP take an examination of how the results?How to query?
四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料
Use DBeaver for mysql data backup and recovery