前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >S3上传代码用例-golang

S3上传代码用例-golang

作者头像
用户1260683
发布2018-03-26 16:21:15
6.4K0
发布2018-03-26 16:21:15
举报

S3上传用例-golang

使用AWS-SDk-golang实现文件上传,支持大文件并发,注意只支持AWS4签名,因此ceph的jewel以上版本才可以用。

代码语言:javascript
复制
package main
import (
    "fmt"
    "os"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/credentials"
    "github.com/aws/aws-sdk-go/service/s3/s3manager"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
)
func main() {
    bucket := aws.String("") //bucket名称
    key := aws.String("testobject") //object keyname
    access_key := ""
    secret_key := ""
    end_point := "http://s3.nie.netease.com" //endpoint设置,不要动
    myContentType := aws.String("application/zip") //content-type设置
    myACL := aws.String("public-read") //acl 设置
    metadata_key := "udf-metadata" //自定义Metadata key
    metadata_value := "abc" //自定义Metadata value
    myMetadata := map[string]*string{
        metadata_key: &metadata_value,
    }
    // Configure to use S3 Server
    s3Config := &aws.Config{
        Credentials:      credentials.NewStaticCredentials(access_key, secret_key, ""),
        Endpoint:         aws.String(end_point),
        Region:           aws.String("us-east-1"),
        DisableSSL:       aws.Bool(true),
        S3ForcePathStyle: aws.Bool(false), //virtual-host style方式,不要修改
    }
    newSession := session.New(s3Config)
    s3Client := s3.New(newSession)
    cparams := &s3.HeadBucketInput{
        Bucket: bucket, // Required
    }
    _, err := s3Client.HeadBucket(cparams)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    uploader := s3manager.NewUploader(newSession)
    filename := "/tmp/demo.pdf" //上传文件路径
    f, err  := os.Open(filename)
    if err != nil {
        fmt.Errorf("failed to open file %q, %v", filename, err)
        return
    }
    // Upload the file to S3.
    result, err := uploader.Upload(&s3manager.UploadInput{
        Bucket: bucket,
        Key:    key,
        Body:   f,
        ContentType: myContentType,
        ACL: myACL,
        Metadata: myMetadata,
    },func(u *s3manager.Uploader) {
        u.PartSize = 10 * 1024 * 1024 // 分块大小,当文件体积超过10M开始进行分块上传
        u.LeavePartsOnError = true
        u.Concurrency = 3}) //并发数
    if err != nil {
        fmt.Printf("Failed to upload data to %s/%s, %s\n", *bucket, *key, err.Error())
        return
    }
    fmt.Printf("file uploaded to, %s\n", result.Location)
    down_file := "/tmp/down_file.pdf" //下载路径
    file, err := os.Create(down_file)
    if err != nil {
        fmt.Println("Failed to create file", err)
        return
    }
    defer file.Close()
    downloader := s3manager.NewDownloader(newSession)
    numBytes, err := downloader.Download(file,
        &s3.GetObjectInput{
            Bucket: bucket,
            Key:    key,
        })
    if err != nil {
        fmt.Println("Failed to download file", err)
        return
    }
    fmt.Println("Downloaded file", file.Name(), numBytes, "bytes")
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-02-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Ceph对象存储方案 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • S3上传用例-golang
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档