Skip to content

Commit

Permalink
Merge pull request #16 from nao1215/feat/s3hub-cp-subcmd
Browse files Browse the repository at this point in the history
Add s3hub cp sub command
  • Loading branch information
nao1215 authored Jan 3, 2024
2 parents 90c48e6 + cf34a69 commit 962b805
Show file tree
Hide file tree
Showing 11 changed files with 682 additions and 197 deletions.
50 changes: 34 additions & 16 deletions app/di/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ type S3App struct {
usecase.S3BucketLister
// S3BucketDeleter is the usecase for deleting a S3 bucket.
usecase.S3BucketDeleter
// S3BucketObjectsLister is the usecase for listing S3 bucket objects.
usecase.S3BucketObjectsLister
// S3BucketObjectsDeleter is the usecase for deleting S3 bucket objects.
usecase.S3BucketObjectsDeleter
// S3ObjectsLister is the usecase for listing S3 bucket objects.
usecase.S3ObjectsLister
// S3ObjectsDeleter is the usecase for deleting S3 bucket objects.
usecase.S3ObjectsDeleter
// S3ObjectUploader is the usecase for uploading a file to S3 bucket.
usecase.S3ObjectDownloader
// FileUploader is the usecase for uploading a file.
usecase.FileUploader
// S3ObjectCopier is the usecase for copying a file in S3 bucket.
usecase.S3ObjectCopier
}

// NewS3App creates a new S3App.
Expand All @@ -37,13 +43,19 @@ func NewS3App(ctx context.Context, profile model.AWSProfile, region model.Region
external.S3BucketListerSet,
external.S3BucketLocationGetterSet,
external.S3BucketDeleterSet,
external.S3BucketObjectsListerSet,
external.S3BucketObjectsDeleterSet,
external.S3ObjectsListerSet,
external.S3ObjectsDeleterSet,
external.S3ObjectDownloaderSet,
external.S3ObjectUploaderSet,
external.S3ObjectCopierSet,
interactor.S3BucketCreatorSet,
interactor.S3BucketListerSet,
interactor.S3BucketDeleterSet,
interactor.S3BucketObjectsListerSet,
interactor.S3BucketObjectsDeleterSet,
interactor.S3ObjectsListerSet,
interactor.S3ObjectsDeleterSet,
interactor.S3ObjectDownloaderSet,
interactor.FileUploaderSet,
interactor.S3ObjectCopierSet,
newS3App,
)
return nil, nil
Expand All @@ -53,15 +65,21 @@ func newS3App(
s3BucketCreator usecase.S3BucketCreator,
s3BucketLister usecase.S3BucketLister,
s3BucketDeleter usecase.S3BucketDeleter,
s3BucketObjectsLister usecase.S3BucketObjectsLister,
s3BucketObjectsDeleter usecase.S3BucketObjectsDeleter,
S3ObjectsLister usecase.S3ObjectsLister,
S3ObjectsDeleter usecase.S3ObjectsDeleter,
s3ObjectDownloader usecase.S3ObjectDownloader,
fileUploader usecase.FileUploader,
s3ObjectCopier usecase.S3ObjectCopier,
) *S3App {
return &S3App{
S3BucketCreator: s3BucketCreator,
S3BucketLister: s3BucketLister,
S3BucketDeleter: s3BucketDeleter,
S3BucketObjectsLister: s3BucketObjectsLister,
S3BucketObjectsDeleter: s3BucketObjectsDeleter,
S3BucketCreator: s3BucketCreator,
S3BucketLister: s3BucketLister,
S3BucketDeleter: s3BucketDeleter,
S3ObjectsLister: S3ObjectsLister,
S3ObjectsDeleter: S3ObjectsDeleter,
S3ObjectDownloader: s3ObjectDownloader,
FileUploader: fileUploader,
S3ObjectCopier: s3ObjectCopier,
}
}

Expand All @@ -88,7 +106,7 @@ func NewSpareApp(ctx context.Context, profile model.AWSProfile, region model.Reg
external.OAICreatorSet,
external.NewS3Client,
external.S3BucketCreatorSet,
external.S3BucketObjectUploaderSet,
external.S3ObjectUploaderSet,
external.S3BucketPublicAccessBlockerSet,
external.S3BucketPolicySetterSet,
interactor.CloudFrontCreatorSet,
Expand Down
60 changes: 42 additions & 18 deletions app/di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions app/domain/model/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ func NewBucketWithoutProtocol(s string) Bucket {
return Bucket(strings.TrimPrefix(s, S3Protocol))
}

// WithProtocol returns the Bucket with the protocol.
func (b Bucket) WithProtocol() Bucket {
return Bucket(S3Protocol + b.String())
}

// Join returns the Bucket with the S3Key.
// e.g. "bucket" + "key" -> "bucket/key"
func (b Bucket) Join(key S3Key) Bucket {
return Bucket(fmt.Sprintf("%s/%s", b.String(), key.String()))
}

// String returns the string representation of the Bucket.
func (b Bucket) String() string {
return string(b)
Expand Down Expand Up @@ -377,6 +388,10 @@ func (k S3Key) IsAll() bool {
return k == "*"
}

func (k S3Key) Join(key S3Key) S3Key {
return S3Key(fmt.Sprintf("%s/%s", k.String(), key))
}

// VersionID is the version ID for the specific version of the object to delete.
// This functionality is not supported for directory buckets.
type VersionID string
Expand Down
90 changes: 59 additions & 31 deletions app/domain/service/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ type S3BucketDeleter interface {
DeleteS3Bucket(ctx context.Context, input *S3BucketDeleterInput) (*S3BucketDeleterOutput, error)
}

// S3BucketObjectsDeleterInput is the input of the DeleteBucketObjects method.
type S3BucketObjectsDeleterInput struct {
// S3ObjectsDeleterInput is the input of the DeleteBucketObjects method.
type S3ObjectsDeleterInput struct {
// Bucket is the name of the bucket to delete.
Bucket model.Bucket
// Region is the region of the bucket that you want to delete.
Expand All @@ -79,52 +79,60 @@ type S3BucketObjectsDeleterInput struct {
S3ObjectSets model.S3ObjectIdentifierSets
}

// S3BucketObjectsDeleterOutput is the output of the DeleteBucketObjects method.
type S3BucketObjectsDeleterOutput struct{}
// S3ObjectsDeleterOutput is the output of the DeleteBucketObjects method.
type S3ObjectsDeleterOutput struct{}

// S3BucketObjectsDeleter is the interface that wraps the basic DeleteBucketObjects method.
type S3BucketObjectsDeleter interface {
DeleteS3BucketObjects(ctx context.Context, input *S3BucketObjectsDeleterInput) (*S3BucketObjectsDeleterOutput, error)
// S3ObjectsDeleter is the interface that wraps the basic DeleteBucketObjects method.
type S3ObjectsDeleter interface {
DeleteS3Objects(ctx context.Context, input *S3ObjectsDeleterInput) (*S3ObjectsDeleterOutput, error)
}

// S3BucketObjectsListerInput is the input of the ListBucketObjects method.
type S3BucketObjectsListerInput struct {
// S3ObjectsListerInput is the input of the ListBucketObjects method.
type S3ObjectsListerInput struct {
// Bucket is the name of the bucket to list.
Bucket model.Bucket
}

// S3BucketObjectsListerOutput is the output of the ListBucketObjects method.
type S3BucketObjectsListerOutput struct {
// S3ObjectsListerOutput is the output of the ListBucketObjects method.
type S3ObjectsListerOutput struct {
// Objects is the list of the objects.
Objects model.S3ObjectIdentifierSets
}

// S3BucketObjectsLister is the interface that wraps the basic ListBucketObjects method.
type S3BucketObjectsLister interface {
ListS3BucketObjects(ctx context.Context, input *S3BucketObjectsListerInput) (*S3BucketObjectsListerOutput, error)
// S3ObjectsLister is the interface that wraps the basic ListBucketObjects method.
type S3ObjectsLister interface {
ListS3Objects(ctx context.Context, input *S3ObjectsListerInput) (*S3ObjectsListerOutput, error)
}

// S3BucketObjectDownloaderInput is the input of the GetBucketObject method.
type S3BucketObjectDownloaderInput struct {
// S3ObjectDownloaderInput is the input of the GetBucketObject method.
type S3ObjectDownloaderInput struct {
// Bucket is the name of the bucket to get.
Bucket model.Bucket
// S3Key is the key of the object to get.
S3Key model.S3Key
// Key is the key of the object to get.
Key model.S3Key
}

// S3BucketObjectDownloaderOutput is the output of the GetBucketObject method.
type S3BucketObjectDownloaderOutput struct {
// S3Object is the object.
// S3ObjectDownloaderOutput is the output of the GetBucketObject method.
type S3ObjectDownloaderOutput struct {
// Bucket is the name of the bucket that you want to download.
Bucket model.Bucket
// Key is the S3 key.
Key model.S3Key
// ContentType is the content type of the downloaded file.
ContentType string
// ContentLength is the content length of the downloaded file.
ContentLength int64
// S3Object is the downloaded object.
S3Object *model.S3Object
}

// S3BucketObjectDownloader is the interface that wraps the basic GetBucketObject method.
type S3BucketObjectDownloader interface {
DownloadS3BucketObject(ctx context.Context, input *S3BucketObjectDownloaderInput) (*S3BucketObjectDownloaderOutput, error)
// S3ObjectDownloader is the interface that wraps the basic GetBucketObject method.
type S3ObjectDownloader interface {
DownloadS3Object(ctx context.Context, input *S3ObjectDownloaderInput) (*S3ObjectDownloaderOutput, error)
}

// S3BucketObjectUploaderInput is the input of the PutBucketObject method.
type S3BucketObjectUploaderInput struct {
// S3ObjectUploaderInput is the input of the PutBucketObject method.
type S3ObjectUploaderInput struct {
// Bucket is the name of the bucket to put.
Bucket model.Bucket
// Region is the region of the bucket that you want to put.
Expand All @@ -135,15 +143,35 @@ type S3BucketObjectUploaderInput struct {
S3Object *model.S3Object
}

// S3BucketObjectUploaderOutput is the output of the PutBucketObject method.
type S3BucketObjectUploaderOutput struct {
// S3ObjectUploaderOutput is the output of the PutBucketObject method.
type S3ObjectUploaderOutput struct {
// ContentType is the content type of the object.
ContentType string
// ContentLength is the size of the object.
ContentLength int64
}

// S3BucketObjectUploader is the interface that wraps the basic PutBucketObject method.
type S3BucketObjectUploader interface {
UploadS3BucketObject(ctx context.Context, input *S3BucketObjectUploaderInput) (*S3BucketObjectUploaderOutput, error)
// S3ObjectUploader is the interface that wraps the basic PutBucketObject method.
type S3ObjectUploader interface {
UploadS3Object(ctx context.Context, input *S3ObjectUploaderInput) (*S3ObjectUploaderOutput, error)
}

// S3ObjectCopierInput is the input of the CopyBucketObject method.
type S3ObjectCopierInput struct {
// SourceBucket is the name of the source bucket.
SourceBucket model.Bucket
// SourceKey is the key of the source object.
SourceKey model.S3Key
// DestinationBucket is the name of the destination bucket.
DestinationBucket model.Bucket
// DestinationKey is the key of the destination object.
DestinationKey model.S3Key
}

// S3ObjectCopierOutput is the output of the CopyBucketObject method.
type S3ObjectCopierOutput struct{}

// S3ObjectCopier is the interface that wraps the basic CopyBucketObject method.
type S3ObjectCopier interface {
CopyS3Object(ctx context.Context, input *S3ObjectCopierInput) (*S3ObjectCopierOutput, error)
}
Loading

0 comments on commit 962b805

Please sign in to comment.