使用Golang从AWS托管的图像中获取*文件

时间:2022-10-22 23:05:55

I can succeffully list all files(jpg photos) inside a S3 Bucket and make an []s3.Objects with it. Now I want to get a pointer to file from one of those photos, but I still cannot download them.

我可以成功地列出S3 Bucket中的所有文件(jpg照片)并使用它制作一个[] s3.Objects。现在我想从其中一张照片中获取指向文件的指针,但我仍然无法下载它们。

package main

import (
    "fmt"
    "log"
    "github.com/aws/aws-sdk-go/aws/credentials"
    "os"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/service/s3"
    //"github.com/aws/aws-sdk-go/service/s3/s3manager"
    "github.com/aws/aws-sdk-go/service/s3/s3manager"
)

func main() {
    acess := "***********"
    secret := "******"
    log.Println(acess + secret)
    creds := credentials.NewStaticCredentials(acess, secret, "")
    _, err := creds.Get()

    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    sess := s3.New(session.New(), &aws.Config{
        Credentials: creds,
        Region: aws.String("sa-east-1"),
        //LogLevel:    aws.LogLevel(aws.LogDebug),
    })

    params := &s3.ListObjectsInput{
        Bucket: aws.String("cescocats"),
    }
    //

    resp, err := sess.ListObjects(params)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    var allPhotos []s3.Object

    //log.Println(resp.String())
    for _, key := range resp.Contents {
        allPhotos = append(allPhotos, *key)
    }

    log.Println(allPhotos)
    // Set up a new s3manager client

    manager := s3manager.NewDownloader(session.New(), func(d *s3manager.Downloader) {
        d.PartSize = 1 * 1024 * 1024 // 64MB per part
    })
    log.Println(manager)
    buff := new([]byte)


    hehe := s3.GetObjectInput{Key:allPhotos[0].Key}
    manager.Download(aws.WriteAtBuffer{}, hehe)

}

How do download a file from S3 using github.com/aws/aws-sdk-go ? Is there any other way? Do I really need to download the file to get its content?

如何使用github.com/aws/aws-sdk-go从S3下载文件?还有别的办法吗?我真的需要下载文件才能获得其内容吗?

1 个解决方案

#1


1  

Try something like this:

尝试这样的事情:

log.Printf("Fetching s3://%v/%v", bucket, key)

buff := &aws.WriteAtBuffer{}
s3dl := s3manager.NewDownloader(sess)
_, err := s3dl.Download(buff, &s3.GetObjectInput{
    Bucket: aws.String(bucket),
    Key:    aws.String(key),
})

if err != nil {
    log.Printf("Could not download from S3: %v", err)
}

Then you can get the contents as buff.Bytes()

然后你可以把内容作为buff.Bytes()

#1


1  

Try something like this:

尝试这样的事情:

log.Printf("Fetching s3://%v/%v", bucket, key)

buff := &aws.WriteAtBuffer{}
s3dl := s3manager.NewDownloader(sess)
_, err := s3dl.Download(buff, &s3.GetObjectInput{
    Bucket: aws.String(bucket),
    Key:    aws.String(key),
})

if err != nil {
    log.Printf("Could not download from S3: %v", err)
}

Then you can get the contents as buff.Bytes()

然后你可以把内容作为buff.Bytes()