当前位置:网站首页>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)

边栏推荐
- Remember a pit for gorm initialization
- MySQL - CRUD operations
- nacos startup error, the database has been configured, stand-alone startup
- 极大似然估计
- 面对职场“毕业”,PM&PMO应该如何从容的应对?如何跳槽能够大幅度升职加薪?
- The failure to create a role in Dahua Westward Journey has been solved
- ros多客户端请求服务
- 永磁同步电机36问(二)——机械量与电物理量如何转化?
- 记一次gorm事务及调试解决mysql死锁
- Nacos源码分析专题(二)-服务注册
猜你喜欢

搭建zabbix监控及邮件报警(超详细教学)

面对职场“毕业”,PM&PMO应该如何从容的应对?如何跳槽能够大幅度升职加薪?

Unable to log in to the Westward Journey

The failure to create a role in Dahua Westward Journey has been solved

一次SQL优化,数据库查询速度提升 60 倍

pyqt上手体验

Use DBeaver for mysql data backup and recovery

Chopper webshell feature analysis

菜刀webshell特征分析

Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
随机推荐
指针数组和数组指针
很有意思的经历,很有意思的项目--文件夹对比工具
240...循迹
What to study after the PMP exam?The soft exam ahead is waiting for you~
53. 最小的k个数
Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
cocos中使用async await异步加载资源
cadence landscape bindkey
BI - SQL 丨 WHILE
NIO's Sword
Curriculum Vitae;CV
Oracle数据类型介绍
ApiFox 基本使用教程(浅尝辄止,非广)
递归检查配置项是否更变并替换
【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
Flask入门学习教程
Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
BI-SQL丨WHILE
2022牛客多校四_G M