storage

package
v1.0.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type S3

type S3 struct {
	Region          string // ie: us-west-2
	AccessKeyID     string
	AccessSecretKey string
	DefaultACL      string // ie: public-read
	BucketName      string // ie: xx-debug
	URL             string // ie: https:xx-debug.s3.us-west-2.amazonaws.com/, https://cdn.xx.com
	CDNHostName     string // ie: cdn.xxx.com

}

func (*S3) AddImageToS3

func (s3 *S3) AddImageToS3(file io.Reader, fileName, bucketDir string, width, height int) (url string, err error)

AddImageToS3 在上传图片到S3的同时,生成指定尺寸的缩略图并上传到S3中的相同目录下。

func (*S3) DeleteFileFromS3

func (s *S3) DeleteFileFromS3(fileURL string)

DeleteFileFromS3 删除S3中的一个文件

func (*S3) DeleteFilesFromS3

func (s *S3) DeleteFilesFromS3(fileURLs []string)

DeleteFilesFromS3 批量删除S3中的文件 https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#S3.DeleteObjects

func (*S3) DeleteImageAndThumbFromS3

func (s3 *S3) DeleteImageAndThumbFromS3(imageURL string)

DeleteImageAndThumbFromS3 删除图片及缩略图(缩略图的命名为http://xxx/xxx_thumb.xx)

func (*S3) DeleteImagesAndThumbsFromS3

func (s3 *S3) DeleteImagesAndThumbsFromS3(imageURLs []string)

func (*S3) DownloadFileFromS3

func (s *S3) DownloadFileFromS3(bucketName, key string) []byte

func (*S3) GenerateThumbsInS3

func (s *S3) GenerateThumbsInS3(bucketName, prefix string, width, height int)

func (*S3) GetCDNURLFromS3URL

func (s3 *S3) GetCDNURLFromS3URL(s3URL string) (cdnURL string)

GetCDNURL 如果是正式服务器,则将s3 URL替换为CDN的url s3 URL的形式有如下几种: 1. https://s3.us-west-2.amazonaws.com/bucketName/dir... 2. https://bucketName.s3.us-west-2.amazonaws.com/dir... 3. https://xxx-debug.s3-us-west-2.amazonaws.com/dir...

func (*S3) GetObjectsListFromS3

func (s3 *S3) GetObjectsListFromS3(bucketName, prefix, startAfter, continuationToken string, objList *[]*s3.Object)

与上面的方法不同的仅仅是:将结果append到list中。且如果被截断了,递归执行,直至全部object都被append到list中

func (*S3) GetPresigndURLForUpload

func (s *S3) GetPresigndURLForUpload(fileName, bucketDir string) (string, error)

https://docs.aws.amazon.com/zh_cn/sdk-for-go/v1/developer-guide/s3-example-presigned-urls.html https://medium.com/@aidan.hallett/securing-aws-s3-uploads-using-presigned-urls-aa821c13ae8d Generate a Pre-Signed URL for an Amazon S3 PUT Operation with a Specific Payload You can generate a pre-signed URL for a PUT operation that checks whether users upload the correct content. When the SDK pre-signs a request, it computes the checksum of the request body and generates an MD5 checksum that is included in the pre-signed URL. Users must upload the same content that produces the same MD5 checksum generated by the SDK; otherwise, the operation fails. This is not the Content-MD5, but the signature. To enforce Content-MD5, simply add the header to the request. fileName 是文件名 backetDir 是上传到bucket里的哪个文件夹,例如 "abc/upload" ! 注意:无法在此时将ACL设为"public-read",需要通过cloudfront来处理

func (*S3) GetPresignedURLForDownload

func (s *S3) GetPresignedURLForDownload(fileName, bucketDir string) (string, error)

func (*S3) GetS3FileNameFromURL

func (s3 *S3) GetS3FileNameFromURL(fileURL string) (fileName string, ok bool)

GetS3FileNameFromURL 将s3 url转换为在s3中的存储路径和文件名 ok: 如果成功转换了文件路径,则返回true,此时fileName为空字符串。 存在S3中的文件url: 正式服务器 https://cdn.xxxx.com/upload/2019/07/04/homepage-ads_CxcMbiF.jpg 测试服务器形式1 https://xxx-debug.s3-us-west-2.amazonaws.com/upload/vehicle/2/report/favicon.png 测试服务器形式2 https://xxx-debug.s3.us-west-2.amazonaws.com/upload/vehicle/2/report/favicon.png 测试服务器形式3 https://s3.us-west-2.amazonaws.com/xxx-debug/upload/vehicle/2/report/favicon.png 测试服务器形式4 https://s3-us-west-2.amazonaws.com/xxx-debug/upload/vehicle/2/report/favicon.png

func (*S3) GetS3URLFromCDNURL

func (s3 *S3) GetS3URLFromCDNURL(cdnURL string) (s3URL string)

GetS3URLFromCDNURL 如果是正式服务器,将图片的cdn URL替换为在S3的真实URL s3的真实url 如:https://xxx-debug.s3-us-west-2.amazonaws.com/dir... 又如:https://s3.us-west-2.amazonaws.com/backetName/user_upload/78/merchant_img/WechatIMG63.jpeg s3 hostname由如下部分构成:"s3." + S3_REGION + ".amazonaws.com/"

func (*S3) ListObjectsFromS3

func (s *S3) ListObjectsFromS3(bucketName, prefix, startAfter, continuationToken string) (result *s3.ListObjectsV2Output)

获得s3 bucket中的文件列表(每次最多1000个) bucketName: bucketName prefix: 例如 "user_upload/" startAfter:从哪个开始,例如"assets/slider/5.JPG" continuationToken:当*result.IsTruncated==true时,会有个nextContinuationToken,用这个来取后面的objects https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#S3.ListObjectsV2 返回值:result

 Contents: [
   {
    ETag: "\"787d14e0d82d995e3615e0900575f951\"",
    Key: "assets/slider/4.JPG",
    LastModified: 2019-07-19 21:52:13 +0000 UTC,
    Size: 90043,
    StorageClass: "STANDARD"
  },
  {
    ETag: "\"7506c19233ddd9a02e29465fc070f0ef\"",
    Key: "assets/slider/5.JPG",
    LastModified: 2019-07-19 21:52:13 +0000 UTC,
    Size: 108746,
    StorageClass: "STANDARD"
  },
  {
    ETag: "\"d41d8cd98f00b204e9800998ecf8427e\"",
    Key: "baby/",
    LastModified: 2019-06-12 17:41:06 +0000 UTC,
    Size: 0,
    StorageClass: "STANDARD"
  }
]
IsTruncated: true,  // 是否被截断了
KeyCount: 6,
MaxKeys: 6,
Name: "classtop-com-test",
NextContinuationToken: "16MrB83O08WqafJ8HqilMdg/iSUSLSJMwZ6on7UtsYC0YWg4lBZHFLQ==",
Prefix: "",
StartAfter: "assets/"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL