You've already forked img-proxy-url-generator
Add browser
This commit is contained in:
35
aws/client.go
Normal file
35
aws/client.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package aws
|
||||
|
||||
import (
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
AwsKey string
|
||||
AwsSecret string
|
||||
AwsRole string
|
||||
Bucket string
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
s3 *s3.S3
|
||||
bucket string
|
||||
}
|
||||
|
||||
func NewClient(config *Config) *Service {
|
||||
awsSession := session.Must(session.NewSession(&aws.Config{
|
||||
Credentials: credentials.NewStaticCredentials(config.AwsKey, config.AwsSecret, ""),
|
||||
Region: aws.String("us-east-1"),
|
||||
}))
|
||||
|
||||
assumeRoleCredentials := stscreds.NewCredentials(awsSession, config.AwsRole)
|
||||
|
||||
return &Service{
|
||||
s3: s3.New(awsSession, &aws.Config{Credentials: assumeRoleCredentials}),
|
||||
bucket: config.Bucket,
|
||||
}
|
||||
}
|
||||
48
aws/s3.go
Normal file
48
aws/s3.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package aws
|
||||
|
||||
import (
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
)
|
||||
|
||||
type Image struct {
|
||||
Url string
|
||||
Download string
|
||||
Name string
|
||||
S3Path string
|
||||
}
|
||||
|
||||
type BucketList struct {
|
||||
Images []Image
|
||||
StartAfter string
|
||||
}
|
||||
|
||||
func (s *Service) ListBucketContents(continuationToken *string) (*BucketList, error) {
|
||||
v2, err := s.s3.ListObjectsV2(&s3.ListObjectsV2Input{
|
||||
Bucket: aws.String(s.bucket),
|
||||
MaxKeys: aws.Int64(20),
|
||||
ContinuationToken: continuationToken,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bucketList := BucketList{
|
||||
Images: make([]Image, 0),
|
||||
}
|
||||
|
||||
if v2.NextContinuationToken != nil {
|
||||
bucketList.StartAfter = *v2.NextContinuationToken
|
||||
}
|
||||
|
||||
for _, item := range v2.Contents {
|
||||
image := Image{
|
||||
Name: *item.Key,
|
||||
S3Path: "s3://" + s.bucket + "/" + *item.Key,
|
||||
}
|
||||
bucketList.Images = append(bucketList.Images, image)
|
||||
}
|
||||
|
||||
return &bucketList, nil
|
||||
}
|
||||
Reference in New Issue
Block a user