• aws s3上传文件


    go get github.com/aws/aws-sdk-go

    package main
    
    import (
    	"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名称"), // bucket名称
    		Key:         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://bucket名称.s3.ap-east-1.amazonaws.com/" + tempFileName
    
    	return fileUrl, err
    }
    
    func main() {
    	s, err := session.NewSession(&aws.Config{
    		Region: aws.String("ap-east-1"), // 替换自己账户的region
    		Credentials: credentials.NewStaticCredentials(
    			"AccessKeyID",                    
    			"SecretAccessKey",       
    			"SessionToken"), // Sessiontoken是进程相关,应该是连接中可以返回 (可为空)
    	})
    	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)
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    AccessKeyID SecretAccessKey: 在账户(或IAM中建个用户)的security_credentials中申请

    在这里插入图片描述

  • 相关阅读:
    Nautilus Chain上线主网,为DeFi和流支付的未来构建基础
    毫米波传感器原理介绍:测距
    【Spring】SpringBoot+Actuator+Prometheus+Grafana监控实现及常见问题处理
    网络运维Day18
    一起来看流星雨啦,哈哈哈哈
    控制输入流,从控制台打印到文件中,更改输出的位置
    CMake入门(一)Ubuntu下使用和Window下使用
    2022-8-25 第七小组 学习日记 (day49)网上点餐系统
    C#正则表达式总结
    Reptile_urllib的基本使用
  • 原文地址:https://blog.csdn.net/qq_41661056/article/details/126040475