s3

package
v0.0.53 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Example (BucketFiles)
package main

import (
	"context"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/sqjian/go-kit/oss/s3"
	"os"
)

var awsConfig *aws.Config

func main() {
	checkErr := func(err error) {
		if err != nil {
			panic(err)
		}
	}

	cli, err := s3.NewS3Cli(
		s3.WithAwsConfig(awsConfig),
		s3.WithProgressOutput(os.Stderr),
	)
	checkErr(err)

	var (
		bucket     = "xxx"
		prefixHint = ""
	)
	{
		files, err := cli.BucketFiles(
			context.Background(),
			bucket,
			prefixHint,
		)
		checkErr(err)
		fmt.Println(files)
	}
}
Output:

Example (DeleteFile)
package main

import (
	"context"
	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/sqjian/go-kit/oss/s3"
	"os"
)

var awsConfig *aws.Config

func main() {
	checkErr := func(err error) {
		if err != nil {
			panic(err)
		}
	}

	cli, err := s3.NewS3Cli(
		s3.WithAwsConfig(awsConfig),
		s3.WithProgressOutput(os.Stdin),
	)
	checkErr(err)

	var (
		bucket    = "xxx"
		objectKey = "xxx"
	)
	{

		err := cli.DeleteFile(
			context.Background(),
			bucket,
			objectKey,
		)
		checkErr(err)
	}
}
Output:

Example (DownloadFile)
package main

import (
	"context"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
	"github.com/sqjian/go-kit/oss/s3"
	"os"
)

var awsConfig *aws.Config

func main() {
	checkErr := func(err error) {
		if err != nil {
			panic(err)
		}
	}

	cli, err := s3.NewS3Cli(
		s3.WithAwsConfig(awsConfig),
		s3.WithProgressOutput(os.Stderr),
	)
	checkErr(err)

	var (
		bucket    = "xxx"
		objectKey = "xxx"
		buf       = manager.NewWriteAtBuffer(nil)
	)
	{

		err := cli.DownloadFile(
			context.Background(),
			bucket,
			objectKey,
			buf,
		)
		checkErr(err)
		fmt.Println(len(buf.Bytes()))
	}
}
Output:

Example (UploadFile)
package main

import (
	"bytes"
	"context"
	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/sqjian/go-kit/oss/s3"
	"os"
)

var awsConfig *aws.Config

func main() {
	checkErr := func(err error) {
		if err != nil {
			panic(err)
		}
	}

	cli, err := s3.NewS3Cli(
		s3.WithAwsConfig(awsConfig),
		s3.WithProgressOutput(os.Stderr),
	)
	checkErr(err)

	var (
		bucket    = "xxx"
		objectKey = "xxx"
		data      = bytes.Repeat([]byte("x"), 1024*1024*1024)
	)
	{
		err := cli.UploadFile(
			context.Background(),
			bucket,
			objectKey,
			s3.NewFsFile("", data, 0644),
		)
		checkErr(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAwsConfig

func NewAwsConfig(opts ...OptionFunc) (*aws.Config, error)

func NewFsFile

func NewFsFile(name string, contents []byte, mode fs.FileMode) fs.File

Types

type BucketListChunk

type BucketListChunk struct {
	Truncated         bool
	ContinuationToken *string
	CommonPrefixes    []string
	Paths             []string
}

type Client

type Client interface {
	BucketFiles(ctx context.Context, bucketName string, prefixHint string) ([]string, error)
	UploadFile(ctx context.Context, bucketName string, remotePath string, localFile fs.File) error
	DownloadFile(ctx context.Context, bucketName string, remotePath string, localFile io.WriterAt) error
	DeleteFile(ctx context.Context, bucketName string, remotePath string) error
}

type Err added in v0.0.52

type Err int
const (
	UnknownErrCode Err = iota
	IllegalParams      // wrong params
)

func (Err) String added in v0.0.52

func (i Err) String() string

type FakeFile

type FakeFile struct {
	// contains filtered or unexported fields
}

FakeFile implements FileLike and also fs.FileInfo.

func (*FakeFile) Close

func (f *FakeFile) Close() error

func (*FakeFile) IsDir

func (f *FakeFile) IsDir() bool

func (*FakeFile) ModTime

func (f *FakeFile) ModTime() time.Time

func (*FakeFile) Mode

func (f *FakeFile) Mode() fs.FileMode

func (*FakeFile) Name

func (f *FakeFile) Name() string

func (*FakeFile) Read

func (f *FakeFile) Read(p []byte) (int, error)

func (*FakeFile) Reset

func (f *FakeFile) Reset() *FakeFile

Reset prepares a FakeFile for reuse.

func (*FakeFile) Size

func (f *FakeFile) Size() int64

func (*FakeFile) Stat

func (f *FakeFile) Stat() (fs.FileInfo, error)

func (*FakeFile) Sys

func (f *FakeFile) Sys() any

type OptionFunc

type OptionFunc func(*s3Config)

func WithAccessKey

func WithAccessKey(accessKey string) OptionFunc

func WithEndpoint

func WithEndpoint(endpoint string) OptionFunc

func WithSecretKey

func WithSecretKey(secretKey string) OptionFunc

type S3client

type S3client struct {
	// contains filtered or unexported fields
}

func NewS3Cli

func NewS3Cli(opts ...S3clientOpt) (*S3client, error)

func (*S3client) BucketFiles

func (c *S3client) BucketFiles(ctx context.Context, bucketName string, directoryPrefix string) ([]string, error)

func (*S3client) DeleteFile

func (c *S3client) DeleteFile(ctx context.Context, bucketName string, remotePath string) error

func (*S3client) DownloadFile

func (c *S3client) DownloadFile(ctx context.Context, bucketName string, remotePath string, localFile io.WriterAt) error

func (*S3client) UploadFile

func (c *S3client) UploadFile(ctx context.Context, bucketName string, remotePath string, localFile fs.File) error

type S3clientOpt

type S3clientOpt interface {
	// contains filtered or unexported methods
}

func WithAwsConfig

func WithAwsConfig(awsConfig *aws.Config) S3clientOpt

func WithProgressOutput

func WithProgressOutput(progressOutput io.Writer) S3clientOpt

Jump to

Keyboard shortcuts

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