0
0
mirror of https://github.com/thegeeklab/wp-s3-action.git synced 2024-06-02 18:39:42 +02:00

switch to package implementation

This commit is contained in:
Robert Kaussow 2024-05-11 23:56:47 +02:00
parent d1efaa5407
commit 81dcc8e3ed
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
11 changed files with 963 additions and 1003 deletions

View File

@ -3,14 +3,22 @@ package aws
import (
"context"
"github.com/aws/aws-sdk-go-v2/service/cloudfront"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
//nolint:lll
type APIClient interface {
type S3APIClient interface {
HeadObject(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error)
PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error)
GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error)
CopyObject(ctx context.Context, params *s3.CopyObjectInput, optFns ...func(*s3.Options)) (*s3.CopyObjectOutput, error)
GetObjectAcl(ctx context.Context, params *s3.GetObjectAclInput, optFns ...func(*s3.Options)) (*s3.GetObjectAclOutput, error)
DeleteObject(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error)
ListObjects(ctx context.Context, params *s3.ListObjectsInput, optFns ...func(*s3.Options)) (*s3.ListObjectsOutput, error)
}
//nolint:lll
type CloudfrontAPIClient interface {
CreateInvalidation(ctx context.Context, params *cloudfront.CreateInvalidationInput, optFns ...func(*cloudfront.Options)) (*cloudfront.CreateInvalidationOutput, error)
}

View File

@ -1,407 +0,0 @@
// Code generated by mockery v2.43.0. DO NOT EDIT.
package mocks
import (
context "context"
s3 "github.com/aws/aws-sdk-go-v2/service/s3"
mock "github.com/stretchr/testify/mock"
)
// MockAPIClient is an autogenerated mock type for the APIClient type
type MockAPIClient struct {
mock.Mock
}
type MockAPIClient_Expecter struct {
mock *mock.Mock
}
func (_m *MockAPIClient) EXPECT() *MockAPIClient_Expecter {
return &MockAPIClient_Expecter{mock: &_m.Mock}
}
// CopyObject provides a mock function with given fields: ctx, params, optFns
func (_m *MockAPIClient) CopyObject(ctx context.Context, params *s3.CopyObjectInput, optFns ...func(*s3.Options)) (*s3.CopyObjectOutput, error) {
_va := make([]interface{}, len(optFns))
for _i := range optFns {
_va[_i] = optFns[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, params)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for CopyObject")
}
var r0 *s3.CopyObjectOutput
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *s3.CopyObjectInput, ...func(*s3.Options)) (*s3.CopyObjectOutput, error)); ok {
return rf(ctx, params, optFns...)
}
if rf, ok := ret.Get(0).(func(context.Context, *s3.CopyObjectInput, ...func(*s3.Options)) *s3.CopyObjectOutput); ok {
r0 = rf(ctx, params, optFns...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*s3.CopyObjectOutput)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *s3.CopyObjectInput, ...func(*s3.Options)) error); ok {
r1 = rf(ctx, params, optFns...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockAPIClient_CopyObject_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CopyObject'
type MockAPIClient_CopyObject_Call struct {
*mock.Call
}
// CopyObject is a helper method to define mock.On call
// - ctx context.Context
// - params *s3.CopyObjectInput
// - optFns ...func(*s3.Options)
func (_e *MockAPIClient_Expecter) CopyObject(ctx interface{}, params interface{}, optFns ...interface{}) *MockAPIClient_CopyObject_Call {
return &MockAPIClient_CopyObject_Call{Call: _e.mock.On("CopyObject",
append([]interface{}{ctx, params}, optFns...)...)}
}
func (_c *MockAPIClient_CopyObject_Call) Run(run func(ctx context.Context, params *s3.CopyObjectInput, optFns ...func(*s3.Options))) *MockAPIClient_CopyObject_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]func(*s3.Options), len(args)-2)
for i, a := range args[2:] {
if a != nil {
variadicArgs[i] = a.(func(*s3.Options))
}
}
run(args[0].(context.Context), args[1].(*s3.CopyObjectInput), variadicArgs...)
})
return _c
}
func (_c *MockAPIClient_CopyObject_Call) Return(_a0 *s3.CopyObjectOutput, _a1 error) *MockAPIClient_CopyObject_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockAPIClient_CopyObject_Call) RunAndReturn(run func(context.Context, *s3.CopyObjectInput, ...func(*s3.Options)) (*s3.CopyObjectOutput, error)) *MockAPIClient_CopyObject_Call {
_c.Call.Return(run)
return _c
}
// GetObject provides a mock function with given fields: ctx, params, optFns
func (_m *MockAPIClient) GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error) {
_va := make([]interface{}, len(optFns))
for _i := range optFns {
_va[_i] = optFns[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, params)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for GetObject")
}
var r0 *s3.GetObjectOutput
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *s3.GetObjectInput, ...func(*s3.Options)) (*s3.GetObjectOutput, error)); ok {
return rf(ctx, params, optFns...)
}
if rf, ok := ret.Get(0).(func(context.Context, *s3.GetObjectInput, ...func(*s3.Options)) *s3.GetObjectOutput); ok {
r0 = rf(ctx, params, optFns...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*s3.GetObjectOutput)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *s3.GetObjectInput, ...func(*s3.Options)) error); ok {
r1 = rf(ctx, params, optFns...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockAPIClient_GetObject_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetObject'
type MockAPIClient_GetObject_Call struct {
*mock.Call
}
// GetObject is a helper method to define mock.On call
// - ctx context.Context
// - params *s3.GetObjectInput
// - optFns ...func(*s3.Options)
func (_e *MockAPIClient_Expecter) GetObject(ctx interface{}, params interface{}, optFns ...interface{}) *MockAPIClient_GetObject_Call {
return &MockAPIClient_GetObject_Call{Call: _e.mock.On("GetObject",
append([]interface{}{ctx, params}, optFns...)...)}
}
func (_c *MockAPIClient_GetObject_Call) Run(run func(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options))) *MockAPIClient_GetObject_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]func(*s3.Options), len(args)-2)
for i, a := range args[2:] {
if a != nil {
variadicArgs[i] = a.(func(*s3.Options))
}
}
run(args[0].(context.Context), args[1].(*s3.GetObjectInput), variadicArgs...)
})
return _c
}
func (_c *MockAPIClient_GetObject_Call) Return(_a0 *s3.GetObjectOutput, _a1 error) *MockAPIClient_GetObject_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockAPIClient_GetObject_Call) RunAndReturn(run func(context.Context, *s3.GetObjectInput, ...func(*s3.Options)) (*s3.GetObjectOutput, error)) *MockAPIClient_GetObject_Call {
_c.Call.Return(run)
return _c
}
// GetObjectAcl provides a mock function with given fields: ctx, params, optFns
func (_m *MockAPIClient) GetObjectAcl(ctx context.Context, params *s3.GetObjectAclInput, optFns ...func(*s3.Options)) (*s3.GetObjectAclOutput, error) {
_va := make([]interface{}, len(optFns))
for _i := range optFns {
_va[_i] = optFns[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, params)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for GetObjectAcl")
}
var r0 *s3.GetObjectAclOutput
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *s3.GetObjectAclInput, ...func(*s3.Options)) (*s3.GetObjectAclOutput, error)); ok {
return rf(ctx, params, optFns...)
}
if rf, ok := ret.Get(0).(func(context.Context, *s3.GetObjectAclInput, ...func(*s3.Options)) *s3.GetObjectAclOutput); ok {
r0 = rf(ctx, params, optFns...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*s3.GetObjectAclOutput)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *s3.GetObjectAclInput, ...func(*s3.Options)) error); ok {
r1 = rf(ctx, params, optFns...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockAPIClient_GetObjectAcl_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetObjectAcl'
type MockAPIClient_GetObjectAcl_Call struct {
*mock.Call
}
// GetObjectAcl is a helper method to define mock.On call
// - ctx context.Context
// - params *s3.GetObjectAclInput
// - optFns ...func(*s3.Options)
func (_e *MockAPIClient_Expecter) GetObjectAcl(ctx interface{}, params interface{}, optFns ...interface{}) *MockAPIClient_GetObjectAcl_Call {
return &MockAPIClient_GetObjectAcl_Call{Call: _e.mock.On("GetObjectAcl",
append([]interface{}{ctx, params}, optFns...)...)}
}
func (_c *MockAPIClient_GetObjectAcl_Call) Run(run func(ctx context.Context, params *s3.GetObjectAclInput, optFns ...func(*s3.Options))) *MockAPIClient_GetObjectAcl_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]func(*s3.Options), len(args)-2)
for i, a := range args[2:] {
if a != nil {
variadicArgs[i] = a.(func(*s3.Options))
}
}
run(args[0].(context.Context), args[1].(*s3.GetObjectAclInput), variadicArgs...)
})
return _c
}
func (_c *MockAPIClient_GetObjectAcl_Call) Return(_a0 *s3.GetObjectAclOutput, _a1 error) *MockAPIClient_GetObjectAcl_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockAPIClient_GetObjectAcl_Call) RunAndReturn(run func(context.Context, *s3.GetObjectAclInput, ...func(*s3.Options)) (*s3.GetObjectAclOutput, error)) *MockAPIClient_GetObjectAcl_Call {
_c.Call.Return(run)
return _c
}
// HeadObject provides a mock function with given fields: ctx, params, optFns
func (_m *MockAPIClient) HeadObject(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error) {
_va := make([]interface{}, len(optFns))
for _i := range optFns {
_va[_i] = optFns[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, params)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for HeadObject")
}
var r0 *s3.HeadObjectOutput
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *s3.HeadObjectInput, ...func(*s3.Options)) (*s3.HeadObjectOutput, error)); ok {
return rf(ctx, params, optFns...)
}
if rf, ok := ret.Get(0).(func(context.Context, *s3.HeadObjectInput, ...func(*s3.Options)) *s3.HeadObjectOutput); ok {
r0 = rf(ctx, params, optFns...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*s3.HeadObjectOutput)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *s3.HeadObjectInput, ...func(*s3.Options)) error); ok {
r1 = rf(ctx, params, optFns...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockAPIClient_HeadObject_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'HeadObject'
type MockAPIClient_HeadObject_Call struct {
*mock.Call
}
// HeadObject is a helper method to define mock.On call
// - ctx context.Context
// - params *s3.HeadObjectInput
// - optFns ...func(*s3.Options)
func (_e *MockAPIClient_Expecter) HeadObject(ctx interface{}, params interface{}, optFns ...interface{}) *MockAPIClient_HeadObject_Call {
return &MockAPIClient_HeadObject_Call{Call: _e.mock.On("HeadObject",
append([]interface{}{ctx, params}, optFns...)...)}
}
func (_c *MockAPIClient_HeadObject_Call) Run(run func(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options))) *MockAPIClient_HeadObject_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]func(*s3.Options), len(args)-2)
for i, a := range args[2:] {
if a != nil {
variadicArgs[i] = a.(func(*s3.Options))
}
}
run(args[0].(context.Context), args[1].(*s3.HeadObjectInput), variadicArgs...)
})
return _c
}
func (_c *MockAPIClient_HeadObject_Call) Return(_a0 *s3.HeadObjectOutput, _a1 error) *MockAPIClient_HeadObject_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockAPIClient_HeadObject_Call) RunAndReturn(run func(context.Context, *s3.HeadObjectInput, ...func(*s3.Options)) (*s3.HeadObjectOutput, error)) *MockAPIClient_HeadObject_Call {
_c.Call.Return(run)
return _c
}
// PutObject provides a mock function with given fields: ctx, params, optFns
func (_m *MockAPIClient) PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
_va := make([]interface{}, len(optFns))
for _i := range optFns {
_va[_i] = optFns[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, params)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for PutObject")
}
var r0 *s3.PutObjectOutput
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *s3.PutObjectInput, ...func(*s3.Options)) (*s3.PutObjectOutput, error)); ok {
return rf(ctx, params, optFns...)
}
if rf, ok := ret.Get(0).(func(context.Context, *s3.PutObjectInput, ...func(*s3.Options)) *s3.PutObjectOutput); ok {
r0 = rf(ctx, params, optFns...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*s3.PutObjectOutput)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *s3.PutObjectInput, ...func(*s3.Options)) error); ok {
r1 = rf(ctx, params, optFns...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockAPIClient_PutObject_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'PutObject'
type MockAPIClient_PutObject_Call struct {
*mock.Call
}
// PutObject is a helper method to define mock.On call
// - ctx context.Context
// - params *s3.PutObjectInput
// - optFns ...func(*s3.Options)
func (_e *MockAPIClient_Expecter) PutObject(ctx interface{}, params interface{}, optFns ...interface{}) *MockAPIClient_PutObject_Call {
return &MockAPIClient_PutObject_Call{Call: _e.mock.On("PutObject",
append([]interface{}{ctx, params}, optFns...)...)}
}
func (_c *MockAPIClient_PutObject_Call) Run(run func(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options))) *MockAPIClient_PutObject_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]func(*s3.Options), len(args)-2)
for i, a := range args[2:] {
if a != nil {
variadicArgs[i] = a.(func(*s3.Options))
}
}
run(args[0].(context.Context), args[1].(*s3.PutObjectInput), variadicArgs...)
})
return _c
}
func (_c *MockAPIClient_PutObject_Call) Return(_a0 *s3.PutObjectOutput, _a1 error) *MockAPIClient_PutObject_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockAPIClient_PutObject_Call) RunAndReturn(run func(context.Context, *s3.PutObjectInput, ...func(*s3.Options)) (*s3.PutObjectOutput, error)) *MockAPIClient_PutObject_Call {
_c.Call.Return(run)
return _c
}
// NewMockAPIClient creates a new instance of MockAPIClient. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewMockAPIClient(t interface {
mock.TestingT
Cleanup(func())
}) *MockAPIClient {
mock := &MockAPIClient{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}

View File

@ -0,0 +1,112 @@
// Code generated by mockery v2.43.0. DO NOT EDIT.
package mocks
import (
context "context"
cloudfront "github.com/aws/aws-sdk-go-v2/service/cloudfront"
mock "github.com/stretchr/testify/mock"
)
// MockCloudfrontAPIClient is an autogenerated mock type for the CloudfrontAPIClient type
type MockCloudfrontAPIClient struct {
mock.Mock
}
type MockCloudfrontAPIClient_Expecter struct {
mock *mock.Mock
}
func (_m *MockCloudfrontAPIClient) EXPECT() *MockCloudfrontAPIClient_Expecter {
return &MockCloudfrontAPIClient_Expecter{mock: &_m.Mock}
}
// CreateInvalidation provides a mock function with given fields: ctx, params, optFns
func (_m *MockCloudfrontAPIClient) CreateInvalidation(ctx context.Context, params *cloudfront.CreateInvalidationInput, optFns ...func(*cloudfront.Options)) (*cloudfront.CreateInvalidationOutput, error) {
_va := make([]interface{}, len(optFns))
for _i := range optFns {
_va[_i] = optFns[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, params)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for CreateInvalidation")
}
var r0 *cloudfront.CreateInvalidationOutput
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *cloudfront.CreateInvalidationInput, ...func(*cloudfront.Options)) (*cloudfront.CreateInvalidationOutput, error)); ok {
return rf(ctx, params, optFns...)
}
if rf, ok := ret.Get(0).(func(context.Context, *cloudfront.CreateInvalidationInput, ...func(*cloudfront.Options)) *cloudfront.CreateInvalidationOutput); ok {
r0 = rf(ctx, params, optFns...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*cloudfront.CreateInvalidationOutput)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *cloudfront.CreateInvalidationInput, ...func(*cloudfront.Options)) error); ok {
r1 = rf(ctx, params, optFns...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockCloudfrontAPIClient_CreateInvalidation_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CreateInvalidation'
type MockCloudfrontAPIClient_CreateInvalidation_Call struct {
*mock.Call
}
// CreateInvalidation is a helper method to define mock.On call
// - ctx context.Context
// - params *cloudfront.CreateInvalidationInput
// - optFns ...func(*cloudfront.Options)
func (_e *MockCloudfrontAPIClient_Expecter) CreateInvalidation(ctx interface{}, params interface{}, optFns ...interface{}) *MockCloudfrontAPIClient_CreateInvalidation_Call {
return &MockCloudfrontAPIClient_CreateInvalidation_Call{Call: _e.mock.On("CreateInvalidation",
append([]interface{}{ctx, params}, optFns...)...)}
}
func (_c *MockCloudfrontAPIClient_CreateInvalidation_Call) Run(run func(ctx context.Context, params *cloudfront.CreateInvalidationInput, optFns ...func(*cloudfront.Options))) *MockCloudfrontAPIClient_CreateInvalidation_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]func(*cloudfront.Options), len(args)-2)
for i, a := range args[2:] {
if a != nil {
variadicArgs[i] = a.(func(*cloudfront.Options))
}
}
run(args[0].(context.Context), args[1].(*cloudfront.CreateInvalidationInput), variadicArgs...)
})
return _c
}
func (_c *MockCloudfrontAPIClient_CreateInvalidation_Call) Return(_a0 *cloudfront.CreateInvalidationOutput, _a1 error) *MockCloudfrontAPIClient_CreateInvalidation_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockCloudfrontAPIClient_CreateInvalidation_Call) RunAndReturn(run func(context.Context, *cloudfront.CreateInvalidationInput, ...func(*cloudfront.Options)) (*cloudfront.CreateInvalidationOutput, error)) *MockCloudfrontAPIClient_CreateInvalidation_Call {
_c.Call.Return(run)
return _c
}
// NewMockCloudfrontAPIClient creates a new instance of MockCloudfrontAPIClient. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewMockCloudfrontAPIClient(t interface {
mock.TestingT
Cleanup(func())
}) *MockCloudfrontAPIClient {
mock := &MockCloudfrontAPIClient{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}

View File

@ -0,0 +1,555 @@
// Code generated by mockery v2.43.0. DO NOT EDIT.
package mocks
import (
context "context"
s3 "github.com/aws/aws-sdk-go-v2/service/s3"
mock "github.com/stretchr/testify/mock"
)
// MockS3APIClient is an autogenerated mock type for the S3APIClient type
type MockS3APIClient struct {
mock.Mock
}
type MockS3APIClient_Expecter struct {
mock *mock.Mock
}
func (_m *MockS3APIClient) EXPECT() *MockS3APIClient_Expecter {
return &MockS3APIClient_Expecter{mock: &_m.Mock}
}
// CopyObject provides a mock function with given fields: ctx, params, optFns
func (_m *MockS3APIClient) CopyObject(ctx context.Context, params *s3.CopyObjectInput, optFns ...func(*s3.Options)) (*s3.CopyObjectOutput, error) {
_va := make([]interface{}, len(optFns))
for _i := range optFns {
_va[_i] = optFns[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, params)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for CopyObject")
}
var r0 *s3.CopyObjectOutput
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *s3.CopyObjectInput, ...func(*s3.Options)) (*s3.CopyObjectOutput, error)); ok {
return rf(ctx, params, optFns...)
}
if rf, ok := ret.Get(0).(func(context.Context, *s3.CopyObjectInput, ...func(*s3.Options)) *s3.CopyObjectOutput); ok {
r0 = rf(ctx, params, optFns...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*s3.CopyObjectOutput)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *s3.CopyObjectInput, ...func(*s3.Options)) error); ok {
r1 = rf(ctx, params, optFns...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockS3APIClient_CopyObject_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CopyObject'
type MockS3APIClient_CopyObject_Call struct {
*mock.Call
}
// CopyObject is a helper method to define mock.On call
// - ctx context.Context
// - params *s3.CopyObjectInput
// - optFns ...func(*s3.Options)
func (_e *MockS3APIClient_Expecter) CopyObject(ctx interface{}, params interface{}, optFns ...interface{}) *MockS3APIClient_CopyObject_Call {
return &MockS3APIClient_CopyObject_Call{Call: _e.mock.On("CopyObject",
append([]interface{}{ctx, params}, optFns...)...)}
}
func (_c *MockS3APIClient_CopyObject_Call) Run(run func(ctx context.Context, params *s3.CopyObjectInput, optFns ...func(*s3.Options))) *MockS3APIClient_CopyObject_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]func(*s3.Options), len(args)-2)
for i, a := range args[2:] {
if a != nil {
variadicArgs[i] = a.(func(*s3.Options))
}
}
run(args[0].(context.Context), args[1].(*s3.CopyObjectInput), variadicArgs...)
})
return _c
}
func (_c *MockS3APIClient_CopyObject_Call) Return(_a0 *s3.CopyObjectOutput, _a1 error) *MockS3APIClient_CopyObject_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockS3APIClient_CopyObject_Call) RunAndReturn(run func(context.Context, *s3.CopyObjectInput, ...func(*s3.Options)) (*s3.CopyObjectOutput, error)) *MockS3APIClient_CopyObject_Call {
_c.Call.Return(run)
return _c
}
// DeleteObject provides a mock function with given fields: ctx, params, optFns
func (_m *MockS3APIClient) DeleteObject(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) {
_va := make([]interface{}, len(optFns))
for _i := range optFns {
_va[_i] = optFns[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, params)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for DeleteObject")
}
var r0 *s3.DeleteObjectOutput
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *s3.DeleteObjectInput, ...func(*s3.Options)) (*s3.DeleteObjectOutput, error)); ok {
return rf(ctx, params, optFns...)
}
if rf, ok := ret.Get(0).(func(context.Context, *s3.DeleteObjectInput, ...func(*s3.Options)) *s3.DeleteObjectOutput); ok {
r0 = rf(ctx, params, optFns...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*s3.DeleteObjectOutput)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *s3.DeleteObjectInput, ...func(*s3.Options)) error); ok {
r1 = rf(ctx, params, optFns...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockS3APIClient_DeleteObject_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'DeleteObject'
type MockS3APIClient_DeleteObject_Call struct {
*mock.Call
}
// DeleteObject is a helper method to define mock.On call
// - ctx context.Context
// - params *s3.DeleteObjectInput
// - optFns ...func(*s3.Options)
func (_e *MockS3APIClient_Expecter) DeleteObject(ctx interface{}, params interface{}, optFns ...interface{}) *MockS3APIClient_DeleteObject_Call {
return &MockS3APIClient_DeleteObject_Call{Call: _e.mock.On("DeleteObject",
append([]interface{}{ctx, params}, optFns...)...)}
}
func (_c *MockS3APIClient_DeleteObject_Call) Run(run func(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options))) *MockS3APIClient_DeleteObject_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]func(*s3.Options), len(args)-2)
for i, a := range args[2:] {
if a != nil {
variadicArgs[i] = a.(func(*s3.Options))
}
}
run(args[0].(context.Context), args[1].(*s3.DeleteObjectInput), variadicArgs...)
})
return _c
}
func (_c *MockS3APIClient_DeleteObject_Call) Return(_a0 *s3.DeleteObjectOutput, _a1 error) *MockS3APIClient_DeleteObject_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockS3APIClient_DeleteObject_Call) RunAndReturn(run func(context.Context, *s3.DeleteObjectInput, ...func(*s3.Options)) (*s3.DeleteObjectOutput, error)) *MockS3APIClient_DeleteObject_Call {
_c.Call.Return(run)
return _c
}
// GetObject provides a mock function with given fields: ctx, params, optFns
func (_m *MockS3APIClient) GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error) {
_va := make([]interface{}, len(optFns))
for _i := range optFns {
_va[_i] = optFns[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, params)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for GetObject")
}
var r0 *s3.GetObjectOutput
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *s3.GetObjectInput, ...func(*s3.Options)) (*s3.GetObjectOutput, error)); ok {
return rf(ctx, params, optFns...)
}
if rf, ok := ret.Get(0).(func(context.Context, *s3.GetObjectInput, ...func(*s3.Options)) *s3.GetObjectOutput); ok {
r0 = rf(ctx, params, optFns...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*s3.GetObjectOutput)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *s3.GetObjectInput, ...func(*s3.Options)) error); ok {
r1 = rf(ctx, params, optFns...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockS3APIClient_GetObject_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetObject'
type MockS3APIClient_GetObject_Call struct {
*mock.Call
}
// GetObject is a helper method to define mock.On call
// - ctx context.Context
// - params *s3.GetObjectInput
// - optFns ...func(*s3.Options)
func (_e *MockS3APIClient_Expecter) GetObject(ctx interface{}, params interface{}, optFns ...interface{}) *MockS3APIClient_GetObject_Call {
return &MockS3APIClient_GetObject_Call{Call: _e.mock.On("GetObject",
append([]interface{}{ctx, params}, optFns...)...)}
}
func (_c *MockS3APIClient_GetObject_Call) Run(run func(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options))) *MockS3APIClient_GetObject_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]func(*s3.Options), len(args)-2)
for i, a := range args[2:] {
if a != nil {
variadicArgs[i] = a.(func(*s3.Options))
}
}
run(args[0].(context.Context), args[1].(*s3.GetObjectInput), variadicArgs...)
})
return _c
}
func (_c *MockS3APIClient_GetObject_Call) Return(_a0 *s3.GetObjectOutput, _a1 error) *MockS3APIClient_GetObject_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockS3APIClient_GetObject_Call) RunAndReturn(run func(context.Context, *s3.GetObjectInput, ...func(*s3.Options)) (*s3.GetObjectOutput, error)) *MockS3APIClient_GetObject_Call {
_c.Call.Return(run)
return _c
}
// GetObjectAcl provides a mock function with given fields: ctx, params, optFns
func (_m *MockS3APIClient) GetObjectAcl(ctx context.Context, params *s3.GetObjectAclInput, optFns ...func(*s3.Options)) (*s3.GetObjectAclOutput, error) {
_va := make([]interface{}, len(optFns))
for _i := range optFns {
_va[_i] = optFns[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, params)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for GetObjectAcl")
}
var r0 *s3.GetObjectAclOutput
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *s3.GetObjectAclInput, ...func(*s3.Options)) (*s3.GetObjectAclOutput, error)); ok {
return rf(ctx, params, optFns...)
}
if rf, ok := ret.Get(0).(func(context.Context, *s3.GetObjectAclInput, ...func(*s3.Options)) *s3.GetObjectAclOutput); ok {
r0 = rf(ctx, params, optFns...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*s3.GetObjectAclOutput)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *s3.GetObjectAclInput, ...func(*s3.Options)) error); ok {
r1 = rf(ctx, params, optFns...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockS3APIClient_GetObjectAcl_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetObjectAcl'
type MockS3APIClient_GetObjectAcl_Call struct {
*mock.Call
}
// GetObjectAcl is a helper method to define mock.On call
// - ctx context.Context
// - params *s3.GetObjectAclInput
// - optFns ...func(*s3.Options)
func (_e *MockS3APIClient_Expecter) GetObjectAcl(ctx interface{}, params interface{}, optFns ...interface{}) *MockS3APIClient_GetObjectAcl_Call {
return &MockS3APIClient_GetObjectAcl_Call{Call: _e.mock.On("GetObjectAcl",
append([]interface{}{ctx, params}, optFns...)...)}
}
func (_c *MockS3APIClient_GetObjectAcl_Call) Run(run func(ctx context.Context, params *s3.GetObjectAclInput, optFns ...func(*s3.Options))) *MockS3APIClient_GetObjectAcl_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]func(*s3.Options), len(args)-2)
for i, a := range args[2:] {
if a != nil {
variadicArgs[i] = a.(func(*s3.Options))
}
}
run(args[0].(context.Context), args[1].(*s3.GetObjectAclInput), variadicArgs...)
})
return _c
}
func (_c *MockS3APIClient_GetObjectAcl_Call) Return(_a0 *s3.GetObjectAclOutput, _a1 error) *MockS3APIClient_GetObjectAcl_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockS3APIClient_GetObjectAcl_Call) RunAndReturn(run func(context.Context, *s3.GetObjectAclInput, ...func(*s3.Options)) (*s3.GetObjectAclOutput, error)) *MockS3APIClient_GetObjectAcl_Call {
_c.Call.Return(run)
return _c
}
// HeadObject provides a mock function with given fields: ctx, params, optFns
func (_m *MockS3APIClient) HeadObject(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options)) (*s3.HeadObjectOutput, error) {
_va := make([]interface{}, len(optFns))
for _i := range optFns {
_va[_i] = optFns[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, params)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for HeadObject")
}
var r0 *s3.HeadObjectOutput
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *s3.HeadObjectInput, ...func(*s3.Options)) (*s3.HeadObjectOutput, error)); ok {
return rf(ctx, params, optFns...)
}
if rf, ok := ret.Get(0).(func(context.Context, *s3.HeadObjectInput, ...func(*s3.Options)) *s3.HeadObjectOutput); ok {
r0 = rf(ctx, params, optFns...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*s3.HeadObjectOutput)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *s3.HeadObjectInput, ...func(*s3.Options)) error); ok {
r1 = rf(ctx, params, optFns...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockS3APIClient_HeadObject_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'HeadObject'
type MockS3APIClient_HeadObject_Call struct {
*mock.Call
}
// HeadObject is a helper method to define mock.On call
// - ctx context.Context
// - params *s3.HeadObjectInput
// - optFns ...func(*s3.Options)
func (_e *MockS3APIClient_Expecter) HeadObject(ctx interface{}, params interface{}, optFns ...interface{}) *MockS3APIClient_HeadObject_Call {
return &MockS3APIClient_HeadObject_Call{Call: _e.mock.On("HeadObject",
append([]interface{}{ctx, params}, optFns...)...)}
}
func (_c *MockS3APIClient_HeadObject_Call) Run(run func(ctx context.Context, params *s3.HeadObjectInput, optFns ...func(*s3.Options))) *MockS3APIClient_HeadObject_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]func(*s3.Options), len(args)-2)
for i, a := range args[2:] {
if a != nil {
variadicArgs[i] = a.(func(*s3.Options))
}
}
run(args[0].(context.Context), args[1].(*s3.HeadObjectInput), variadicArgs...)
})
return _c
}
func (_c *MockS3APIClient_HeadObject_Call) Return(_a0 *s3.HeadObjectOutput, _a1 error) *MockS3APIClient_HeadObject_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockS3APIClient_HeadObject_Call) RunAndReturn(run func(context.Context, *s3.HeadObjectInput, ...func(*s3.Options)) (*s3.HeadObjectOutput, error)) *MockS3APIClient_HeadObject_Call {
_c.Call.Return(run)
return _c
}
// ListObjects provides a mock function with given fields: ctx, params, optFns
func (_m *MockS3APIClient) ListObjects(ctx context.Context, params *s3.ListObjectsInput, optFns ...func(*s3.Options)) (*s3.ListObjectsOutput, error) {
_va := make([]interface{}, len(optFns))
for _i := range optFns {
_va[_i] = optFns[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, params)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for ListObjects")
}
var r0 *s3.ListObjectsOutput
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *s3.ListObjectsInput, ...func(*s3.Options)) (*s3.ListObjectsOutput, error)); ok {
return rf(ctx, params, optFns...)
}
if rf, ok := ret.Get(0).(func(context.Context, *s3.ListObjectsInput, ...func(*s3.Options)) *s3.ListObjectsOutput); ok {
r0 = rf(ctx, params, optFns...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*s3.ListObjectsOutput)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *s3.ListObjectsInput, ...func(*s3.Options)) error); ok {
r1 = rf(ctx, params, optFns...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockS3APIClient_ListObjects_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListObjects'
type MockS3APIClient_ListObjects_Call struct {
*mock.Call
}
// ListObjects is a helper method to define mock.On call
// - ctx context.Context
// - params *s3.ListObjectsInput
// - optFns ...func(*s3.Options)
func (_e *MockS3APIClient_Expecter) ListObjects(ctx interface{}, params interface{}, optFns ...interface{}) *MockS3APIClient_ListObjects_Call {
return &MockS3APIClient_ListObjects_Call{Call: _e.mock.On("ListObjects",
append([]interface{}{ctx, params}, optFns...)...)}
}
func (_c *MockS3APIClient_ListObjects_Call) Run(run func(ctx context.Context, params *s3.ListObjectsInput, optFns ...func(*s3.Options))) *MockS3APIClient_ListObjects_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]func(*s3.Options), len(args)-2)
for i, a := range args[2:] {
if a != nil {
variadicArgs[i] = a.(func(*s3.Options))
}
}
run(args[0].(context.Context), args[1].(*s3.ListObjectsInput), variadicArgs...)
})
return _c
}
func (_c *MockS3APIClient_ListObjects_Call) Return(_a0 *s3.ListObjectsOutput, _a1 error) *MockS3APIClient_ListObjects_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockS3APIClient_ListObjects_Call) RunAndReturn(run func(context.Context, *s3.ListObjectsInput, ...func(*s3.Options)) (*s3.ListObjectsOutput, error)) *MockS3APIClient_ListObjects_Call {
_c.Call.Return(run)
return _c
}
// PutObject provides a mock function with given fields: ctx, params, optFns
func (_m *MockS3APIClient) PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
_va := make([]interface{}, len(optFns))
for _i := range optFns {
_va[_i] = optFns[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, params)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
if len(ret) == 0 {
panic("no return value specified for PutObject")
}
var r0 *s3.PutObjectOutput
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, *s3.PutObjectInput, ...func(*s3.Options)) (*s3.PutObjectOutput, error)); ok {
return rf(ctx, params, optFns...)
}
if rf, ok := ret.Get(0).(func(context.Context, *s3.PutObjectInput, ...func(*s3.Options)) *s3.PutObjectOutput); ok {
r0 = rf(ctx, params, optFns...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*s3.PutObjectOutput)
}
}
if rf, ok := ret.Get(1).(func(context.Context, *s3.PutObjectInput, ...func(*s3.Options)) error); ok {
r1 = rf(ctx, params, optFns...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockS3APIClient_PutObject_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'PutObject'
type MockS3APIClient_PutObject_Call struct {
*mock.Call
}
// PutObject is a helper method to define mock.On call
// - ctx context.Context
// - params *s3.PutObjectInput
// - optFns ...func(*s3.Options)
func (_e *MockS3APIClient_Expecter) PutObject(ctx interface{}, params interface{}, optFns ...interface{}) *MockS3APIClient_PutObject_Call {
return &MockS3APIClient_PutObject_Call{Call: _e.mock.On("PutObject",
append([]interface{}{ctx, params}, optFns...)...)}
}
func (_c *MockS3APIClient_PutObject_Call) Run(run func(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options))) *MockS3APIClient_PutObject_Call {
_c.Call.Run(func(args mock.Arguments) {
variadicArgs := make([]func(*s3.Options), len(args)-2)
for i, a := range args[2:] {
if a != nil {
variadicArgs[i] = a.(func(*s3.Options))
}
}
run(args[0].(context.Context), args[1].(*s3.PutObjectInput), variadicArgs...)
})
return _c
}
func (_c *MockS3APIClient_PutObject_Call) Return(_a0 *s3.PutObjectOutput, _a1 error) *MockS3APIClient_PutObject_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockS3APIClient_PutObject_Call) RunAndReturn(run func(context.Context, *s3.PutObjectInput, ...func(*s3.Options)) (*s3.PutObjectOutput, error)) *MockS3APIClient_PutObject_Call {
_c.Call.Return(run)
return _c
}
// NewMockS3APIClient creates a new instance of MockS3APIClient. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewMockS3APIClient(t interface {
mock.TestingT
Cleanup(func())
}) *MockS3APIClient {
mock := &MockS3APIClient{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}

230
aws/s3.go
View File

@ -9,38 +9,62 @@ import (
"mime"
"os"
"path/filepath"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/cloudfront"
cf_types "github.com/aws/aws-sdk-go-v2/service/cloudfront/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
s3_types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/rs/zerolog/log"
)
type Client struct {
client APIClient
cloudfront *cloudfront.Client
S3 *S3
Cloudfront *Cloudfront
}
type S3Uploader struct {
client APIClient
Opt S3UploaderOpt
type Cloudfront struct {
client CloudfrontAPIClient
Distribution string
}
type S3UploaderOpt struct {
Local string
Remote string
type CloudfrontInvalidateOpt struct {
Path string
}
type S3 struct {
client S3APIClient
Bucket string
DryRun bool
}
type S3UploadOpt struct {
LocalFilePath string
RemoteObjectKey string
ACL map[string]string
ContentType map[string]string
ContentEncoding map[string]string
CacheControl map[string]string
Metadata map[string]map[string]string
Bucket string
DryRun bool
}
type S3RedirectOpt struct {
Path string
Location string
}
type S3DeleteOpt struct {
RemoteObjectKey string
}
type S3ListOpt struct {
Path string
}
// NewClient creates a new S3 client with the provided configuration.
func NewClient(ctx context.Context, url, region, accessKey, secretKey string, pathStyle bool) (*Client, error) {
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
if err != nil {
@ -62,59 +86,59 @@ func NewClient(ctx context.Context, url, region, accessKey, secretKey string, pa
cf := cloudfront.NewFromConfig(cfg)
return &Client{
client: c,
cloudfront: cf,
S3: &S3{client: c},
Cloudfront: &Cloudfront{client: cf},
}, nil
}
// Upload uploads a file to an S3 bucket. It first checks if the file already exists in the
// bucket and if the content and metadata have not changed. If the file has not changed,
// it skips the upload and just updates the metadata. If the file has changed, it uploads
// the new file to the bucket.
func (u *S3Uploader) Upload() error {
if u.Opt.Local == "" {
// Upload uploads a file to an S3 bucket. It first checks if the file already exists in the bucket
// and compares the local file's content and metadata with the remote file. If the file has changed,
// it updates the remote file's metadata. If the file does not exist or has changed,
// it uploads the local file to the remote bucket.
func (u *S3) Upload(ctx context.Context, opt S3UploadOpt) error {
if opt.LocalFilePath == "" {
return nil
}
file, err := os.Open(u.Opt.Local)
file, err := os.Open(opt.LocalFilePath)
if err != nil {
return err
}
defer file.Close()
acl := getACL(u.Opt.Local, u.Opt.ACL)
contentType := getContentType(u.Opt.Local, u.Opt.ContentType)
contentEncoding := getContentEncoding(u.Opt.Local, u.Opt.ContentEncoding)
cacheControl := getCacheControl(u.Opt.Local, u.Opt.CacheControl)
metadata := getMetadata(u.Opt.Local, u.Opt.Metadata)
acl := getACL(opt.LocalFilePath, opt.ACL)
contentType := getContentType(opt.LocalFilePath, opt.ContentType)
contentEncoding := getContentEncoding(opt.LocalFilePath, opt.ContentEncoding)
cacheControl := getCacheControl(opt.LocalFilePath, opt.CacheControl)
metadata := getMetadata(opt.LocalFilePath, opt.Metadata)
head, err := u.client.HeadObject(context.TODO(), &s3.HeadObjectInput{
Bucket: &u.Opt.Bucket,
Key: &u.Opt.Remote,
head, err := u.client.HeadObject(ctx, &s3.HeadObjectInput{
Bucket: &u.Bucket,
Key: &opt.RemoteObjectKey,
})
if err != nil {
var noSuchKeyError *types.NoSuchKey
var noSuchKeyError *s3_types.NoSuchKey
if !errors.As(err, &noSuchKeyError) {
return err
}
log.Debug().Msgf(
"'%s' not found in bucket, uploading with content-type '%s' and permissions '%s'",
u.Opt.Local,
opt.LocalFilePath,
contentType,
acl,
)
if u.Opt.DryRun {
if u.DryRun {
return nil
}
_, err = u.client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: &u.Opt.Bucket,
Key: &u.Opt.Remote,
_, err = u.client.PutObject(ctx, &s3.PutObjectInput{
Bucket: &u.Bucket,
Key: &opt.RemoteObjectKey,
Body: file,
ContentType: &contentType,
ACL: types.ObjectCannedACL(acl),
ACL: s3_types.ObjectCannedACL(acl),
Metadata: metadata,
CacheControl: &cacheControl,
ContentEncoding: &contentEncoding,
@ -129,27 +153,29 @@ func (u *S3Uploader) Upload() error {
sum := fmt.Sprintf("'%x'", hash.Sum(nil))
if sum == *head.ETag {
shouldCopy, reason := u.shouldCopyObject(head, contentType, acl, contentEncoding, cacheControl, metadata)
shouldCopy, reason := u.shouldCopyObject(
ctx, head, opt.LocalFilePath, opt.RemoteObjectKey, contentType, acl, contentEncoding, cacheControl, metadata,
)
if !shouldCopy {
log.Debug().Msgf("skipping '%s' because hashes and metadata match", u.Opt.Local)
log.Debug().Msgf("skipping '%s' because hashes and metadata match", opt.LocalFilePath)
return nil
}
log.Debug().Msgf("updating metadata for '%s' %s", u.Opt.Local, reason)
log.Debug().Msgf("updating metadata for '%s' %s", opt.LocalFilePath, reason)
if u.Opt.DryRun {
if u.DryRun {
return nil
}
_, err = u.client.CopyObject(context.Background(), &s3.CopyObjectInput{
Bucket: &u.Opt.Bucket,
Key: &u.Opt.Remote,
CopySource: aws.String(fmt.Sprintf("%s/%s", u.Opt.Bucket, u.Opt.Remote)),
ACL: types.ObjectCannedACL(acl),
_, err = u.client.CopyObject(ctx, &s3.CopyObjectInput{
Bucket: &u.Bucket,
Key: &opt.RemoteObjectKey,
CopySource: aws.String(fmt.Sprintf("%s/%s", u.Bucket, opt.RemoteObjectKey)),
ACL: s3_types.ObjectCannedACL(acl),
ContentType: &contentType,
Metadata: metadata,
MetadataDirective: types.MetadataDirectiveReplace,
MetadataDirective: s3_types.MetadataDirectiveReplace,
CacheControl: &cacheControl,
ContentEncoding: &contentEncoding,
})
@ -162,18 +188,18 @@ func (u *S3Uploader) Upload() error {
return err
}
log.Debug().Msgf("uploading '%s' with content-type '%s' and permissions '%s'", u.Opt.Local, contentType, acl)
log.Debug().Msgf("uploading '%s' with content-type '%s' and permissions '%s'", opt.LocalFilePath, contentType, acl)
if u.Opt.DryRun {
if u.DryRun {
return nil
}
_, err = u.client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: &u.Opt.Bucket,
Key: &u.Opt.Remote,
_, err = u.client.PutObject(ctx, &s3.PutObjectInput{
Bucket: &u.Bucket,
Key: &opt.RemoteObjectKey,
Body: file,
ContentType: &contentType,
ACL: types.ObjectCannedACL(acl),
ACL: s3_types.ObjectCannedACL(acl),
Metadata: metadata,
CacheControl: &cacheControl,
ContentEncoding: &contentEncoding,
@ -188,8 +214,10 @@ func (u *S3Uploader) Upload() error {
// along with a string describing the reason for the copy if applicable.
//
//nolint:gocognit
func (u *S3Uploader) shouldCopyObject(
head *s3.HeadObjectOutput, contentType, acl, contentEncoding, cacheControl string, metadata map[string]string,
func (u *S3) shouldCopyObject(
ctx context.Context, head *s3.HeadObjectOutput,
local, remote, contentType, acl, contentEncoding, cacheControl string,
metadata map[string]string,
) (bool, string) {
var reason string
@ -230,7 +258,7 @@ func (u *S3Uploader) shouldCopyObject(
}
if len(head.Metadata) != len(metadata) {
reason = fmt.Sprintf("count of metadata values has changed for %s", u.Opt.Local)
reason = fmt.Sprintf("count of metadata values has changed for %s", local)
return true, reason
}
@ -239,7 +267,7 @@ func (u *S3Uploader) shouldCopyObject(
for k, v := range metadata {
if hv, ok := head.Metadata[k]; ok {
if v != hv {
reason = fmt.Sprintf("metadata values have changed for %s", u.Opt.Local)
reason = fmt.Sprintf("metadata values have changed for %s", remote)
return true, reason
}
@ -247,9 +275,9 @@ func (u *S3Uploader) shouldCopyObject(
}
}
grant, err := u.client.GetObjectAcl(context.TODO(), &s3.GetObjectAclInput{
Bucket: &u.Opt.Bucket,
Key: &u.Opt.Remote,
grant, err := u.client.GetObjectAcl(ctx, &s3.GetObjectAclInput{
Bucket: &u.Bucket,
Key: &remote,
})
if err != nil {
return false, ""
@ -276,7 +304,7 @@ func (u *S3Uploader) shouldCopyObject(
}
if previousACL != acl {
reason = fmt.Sprintf("permissions for '%s' have changed from '%s' to '%s'", u.Opt.Remote, previousACL, acl)
reason = fmt.Sprintf("permissions for '%s' have changed from '%s' to '%s'", remote, previousACL, acl)
return true, reason
}
@ -342,3 +370,87 @@ func getMetadata(file string, patterns map[string]map[string]string) map[string]
return metadata
}
func (u *S3) Redirect(ctx context.Context, opt S3RedirectOpt) error {
log.Debug().Msgf("adding redirect from '%s' to '%s'", opt.Path, opt.Location)
if u.DryRun {
return nil
}
_, err := u.client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(u.Bucket),
Key: aws.String(opt.Path),
ACL: s3_types.ObjectCannedACLPublicRead,
WebsiteRedirectLocation: aws.String(opt.Location),
})
return err
}
func (u *S3) Delete(ctx context.Context, opt S3DeleteOpt) error {
log.Debug().Msgf("removing remote file '%s'", opt.RemoteObjectKey)
if u.DryRun {
return nil
}
_, err := u.client.DeleteObject(ctx, &s3.DeleteObjectInput{
Bucket: aws.String(u.Bucket),
Key: aws.String(opt.RemoteObjectKey),
})
return err
}
func (u *S3) List(ctx context.Context, opt S3ListOpt) ([]string, error) {
remote := make([]string, 0)
resp, err := u.client.ListObjects(ctx, &s3.ListObjectsInput{
Bucket: aws.String(u.Bucket),
Prefix: aws.String(opt.Path),
})
if err != nil {
return remote, err
}
for _, item := range resp.Contents {
remote = append(remote, *item.Key)
}
for *resp.IsTruncated {
resp, err = u.client.ListObjects(ctx, &s3.ListObjectsInput{
Bucket: aws.String(u.Bucket),
Prefix: aws.String(opt.Path),
Marker: aws.String(remote[len(remote)-1]),
})
if err != nil {
return remote, err
}
for _, item := range resp.Contents {
remote = append(remote, *item.Key)
}
}
return remote, nil
}
func (c *Cloudfront) Invalidate(ctx context.Context, opt CloudfrontInvalidateOpt) error {
log.Debug().Msgf("invalidating '%s'", opt.Path)
_, err := c.client.CreateInvalidation(ctx, &cloudfront.CreateInvalidationInput{
DistributionId: aws.String(c.Distribution),
InvalidationBatch: &cf_types.InvalidationBatch{
CallerReference: aws.String(time.Now().Format(time.RFC3339Nano)),
Paths: &cf_types.Paths{
Quantity: aws.Int32(1),
Items: []string{
opt.Path,
},
},
},
})
return err
}

View File

@ -1,6 +1,7 @@
package aws
import (
"context"
"os"
"path/filepath"
"testing"
@ -18,51 +19,48 @@ func TestS3Uploader_Upload(t *testing.T) {
tests := []struct {
name string
setup func(t *testing.T) (*S3Uploader, func())
setup func(t *testing.T) (*S3, S3UploadOpt, func())
wantErr bool
}{
{
name: "skip_upload_when_local_is_empty",
setup: func(t *testing.T) (*S3Uploader, func()) {
setup: func(t *testing.T) (*S3, S3UploadOpt, func()) {
t.Helper()
return &S3Uploader{
Opt: S3UploaderOpt{
Local: "",
},
}, func() {}
return &S3{},
S3UploadOpt{
LocalFilePath: "",
}, func() {}
},
wantErr: false,
},
{
name: "error_when_local_file_does_not_exist",
setup: func(t *testing.T) (*S3Uploader, func()) {
setup: func(t *testing.T) (*S3, S3UploadOpt, func()) {
t.Helper()
return &S3Uploader{
Opt: S3UploaderOpt{
Local: "/path/to/non-existent/file",
},
}, func() {}
return &S3{},
S3UploadOpt{
LocalFilePath: "/path/to/non-existent/file",
}, func() {}
},
wantErr: true,
},
{
name: "upload_new_file_with_default_acl_and_content_type",
setup: func(t *testing.T) (*S3Uploader, func()) {
setup: func(t *testing.T) (*S3, S3UploadOpt, func()) {
t.Helper()
mockS3Client := mocks.NewMockAPIClient(t)
mockS3Client := mocks.NewMockS3APIClient(t)
mockS3Client.On("HeadObject", mock.Anything, mock.Anything).Return(&s3.HeadObjectOutput{}, &s3_types.NoSuchKey{})
mockS3Client.On("PutObject", mock.Anything, mock.Anything).Return(&s3.PutObjectOutput{}, nil)
return &S3Uploader{
return &S3{
client: mockS3Client,
Opt: S3UploaderOpt{
Local: createTempFile(t, "file.txt"),
Bucket: "test-bucket",
Remote: "remote/path/file.txt",
},
Bucket: "test-bucket",
}, S3UploadOpt{
LocalFilePath: createTempFile(t, "file.txt"),
RemoteObjectKey: "remote/path/file.txt",
}, func() {
mockS3Client.AssertExpectations(t)
}
@ -71,24 +69,23 @@ func TestS3Uploader_Upload(t *testing.T) {
},
{
name: "update_metadata_when_content_type_changed",
setup: func(t *testing.T) (*S3Uploader, func()) {
setup: func(t *testing.T) (*S3, S3UploadOpt, func()) {
t.Helper()
mockS3Client := mocks.NewMockAPIClient(t)
mockS3Client := mocks.NewMockS3APIClient(t)
mockS3Client.On("HeadObject", mock.Anything, mock.Anything).Return(&s3.HeadObjectOutput{
ETag: aws.String("'5d41402abc4b2a76b9719d911017c592'"),
ContentType: aws.String("application/octet-stream"),
}, nil)
mockS3Client.On("CopyObject", mock.Anything, mock.Anything).Return(&s3.CopyObjectOutput{}, nil)
return &S3Uploader{
return &S3{
client: mockS3Client,
Opt: S3UploaderOpt{
Local: createTempFile(t, "file.txt"),
Bucket: "test-bucket",
Remote: "remote/path/file.txt",
ContentType: map[string]string{"*.txt": "text/plain"},
},
Bucket: "test-bucket",
}, S3UploadOpt{
LocalFilePath: createTempFile(t, "file.txt"),
RemoteObjectKey: "remote/path/file.txt",
ContentType: map[string]string{"*.txt": "text/plain"},
}, func() {
mockS3Client.AssertExpectations(t)
}
@ -97,10 +94,10 @@ func TestS3Uploader_Upload(t *testing.T) {
},
{
name: "update_metadata_when_acl_changed",
setup: func(t *testing.T) (*S3Uploader, func()) {
setup: func(t *testing.T) (*S3, S3UploadOpt, func()) {
t.Helper()
mockS3Client := mocks.NewMockAPIClient(t)
mockS3Client := mocks.NewMockS3APIClient(t)
mockS3Client.On("HeadObject", mock.Anything, mock.Anything).Return(&s3.HeadObjectOutput{
ETag: aws.String("'5d41402abc4b2a76b9719d911017c592'"),
ContentType: aws.String("text/plain; charset=utf-8"),
@ -117,14 +114,13 @@ func TestS3Uploader_Upload(t *testing.T) {
}, nil)
mockS3Client.On("CopyObject", mock.Anything, mock.Anything).Return(&s3.CopyObjectOutput{}, nil)
return &S3Uploader{
return &S3{
client: mockS3Client,
Opt: S3UploaderOpt{
Local: createTempFile(t, "file.txt"),
Bucket: "test-bucket",
Remote: "remote/path/file.txt",
ACL: map[string]string{"*.txt": "public-read"},
},
Bucket: "test-bucket",
}, S3UploadOpt{
LocalFilePath: createTempFile(t, "file.txt"),
RemoteObjectKey: "remote/path/file.txt",
ACL: map[string]string{"*.txt": "public-read"},
}, func() {
mockS3Client.AssertExpectations(t)
}
@ -133,10 +129,10 @@ func TestS3Uploader_Upload(t *testing.T) {
},
{
name: "update_metadata_when_cache_control_changed",
setup: func(t *testing.T) (*S3Uploader, func()) {
setup: func(t *testing.T) (*S3, S3UploadOpt, func()) {
t.Helper()
mockS3Client := mocks.NewMockAPIClient(t)
mockS3Client := mocks.NewMockS3APIClient(t)
mockS3Client.On("HeadObject", mock.Anything, mock.Anything).Return(&s3.HeadObjectOutput{
ETag: aws.String("'5d41402abc4b2a76b9719d911017c592'"),
ContentType: aws.String("text/plain; charset=utf-8"),
@ -144,14 +140,13 @@ func TestS3Uploader_Upload(t *testing.T) {
}, nil)
mockS3Client.On("CopyObject", mock.Anything, mock.Anything).Return(&s3.CopyObjectOutput{}, nil)
return &S3Uploader{
return &S3{
client: mockS3Client,
Opt: S3UploaderOpt{
Local: createTempFile(t, "file.txt"),
Bucket: "test-bucket",
Remote: "remote/path/file.txt",
CacheControl: map[string]string{"*.txt": "max-age=3600"},
},
Bucket: "test-bucket",
}, S3UploadOpt{
LocalFilePath: createTempFile(t, "file.txt"),
RemoteObjectKey: "remote/path/file.txt",
CacheControl: map[string]string{"*.txt": "max-age=3600"},
}, func() {
mockS3Client.AssertExpectations(t)
}
@ -160,10 +155,10 @@ func TestS3Uploader_Upload(t *testing.T) {
},
{
name: "update_metadata_when_content_encoding_changed",
setup: func(t *testing.T) (*S3Uploader, func()) {
setup: func(t *testing.T) (*S3, S3UploadOpt, func()) {
t.Helper()
mockS3Client := mocks.NewMockAPIClient(t)
mockS3Client := mocks.NewMockS3APIClient(t)
mockS3Client.On("HeadObject", mock.Anything, mock.Anything).Return(&s3.HeadObjectOutput{
ETag: aws.String("'5d41402abc4b2a76b9719d911017c592'"),
ContentType: aws.String("text/plain; charset=utf-8"),
@ -171,14 +166,13 @@ func TestS3Uploader_Upload(t *testing.T) {
}, nil)
mockS3Client.On("CopyObject", mock.Anything, mock.Anything).Return(&s3.CopyObjectOutput{}, nil)
return &S3Uploader{
return &S3{
client: mockS3Client,
Opt: S3UploaderOpt{
Local: createTempFile(t, "file.txt"),
Bucket: "test-bucket",
Remote: "remote/path/file.txt",
ContentEncoding: map[string]string{"*.txt": "gzip"},
},
Bucket: "test-bucket",
}, S3UploadOpt{
LocalFilePath: createTempFile(t, "file.txt"),
RemoteObjectKey: "remote/path/file.txt",
ContentEncoding: map[string]string{"*.txt": "gzip"},
}, func() {
mockS3Client.AssertExpectations(t)
}
@ -187,10 +181,10 @@ func TestS3Uploader_Upload(t *testing.T) {
},
{
name: "update_metadata_when_metadata_changed",
setup: func(t *testing.T) (*S3Uploader, func()) {
setup: func(t *testing.T) (*S3, S3UploadOpt, func()) {
t.Helper()
mockS3Client := mocks.NewMockAPIClient(t)
mockS3Client := mocks.NewMockS3APIClient(t)
mockS3Client.On("HeadObject", mock.Anything, mock.Anything).Return(&s3.HeadObjectOutput{
ETag: aws.String("'5d41402abc4b2a76b9719d911017c592'"),
ContentType: aws.String("text/plain; charset=utf-8"),
@ -198,14 +192,13 @@ func TestS3Uploader_Upload(t *testing.T) {
}, nil)
mockS3Client.On("CopyObject", mock.Anything, mock.Anything).Return(&s3.CopyObjectOutput{}, nil)
return &S3Uploader{
return &S3{
client: mockS3Client,
Opt: S3UploaderOpt{
Local: createTempFile(t, "file.txt"),
Bucket: "test-bucket",
Remote: "remote/path/file.txt",
Metadata: map[string]map[string]string{"*.txt": {"key": "value"}},
},
Bucket: "test-bucket",
}, S3UploadOpt{
LocalFilePath: createTempFile(t, "file.txt"),
RemoteObjectKey: "remote/path/file.txt",
Metadata: map[string]map[string]string{"*.txt": {"key": "value"}},
}, func() {
mockS3Client.AssertExpectations(t)
}
@ -214,20 +207,19 @@ func TestS3Uploader_Upload(t *testing.T) {
},
{
name: "upload_new_file_when_dry_run_is_true",
setup: func(t *testing.T) (*S3Uploader, func()) {
setup: func(t *testing.T) (*S3, S3UploadOpt, func()) {
t.Helper()
mockS3Client := mocks.NewMockAPIClient(t)
mockS3Client := mocks.NewMockS3APIClient(t)
mockS3Client.On("HeadObject", mock.Anything, mock.Anything).Return(&s3.HeadObjectOutput{}, &s3_types.NoSuchKey{})
return &S3Uploader{
return &S3{
client: mockS3Client,
Opt: S3UploaderOpt{
Local: createTempFile(t, "file1.txt"),
Bucket: "test-bucket",
Remote: "remote/path/file1.txt",
DryRun: true,
},
Bucket: "test-bucket",
DryRun: true,
}, S3UploadOpt{
LocalFilePath: createTempFile(t, "file1.txt"),
RemoteObjectKey: "remote/path/file1.txt",
}, func() {
mockS3Client.AssertExpectations(t)
}
@ -241,10 +233,10 @@ func TestS3Uploader_Upload(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
uploader, teardown := tt.setup(t)
upload, opt, teardown := tt.setup(t)
defer teardown()
err := uploader.Upload()
err := upload.Upload(context.Background(), opt)
if tt.wantErr {
assert.Error(t, err)

1
go.mod
View File

@ -9,7 +9,6 @@ require (
github.com/aws/aws-sdk-go-v2/service/cloudfront v1.36.1
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.2
github.com/rs/zerolog v1.32.0
github.com/ryanuber/go-glob v1.0.0
github.com/stretchr/testify v1.9.0
github.com/thegeeklab/wp-plugin-go/v2 v2.3.1
github.com/urfave/cli/v2 v2.27.2

2
go.sum
View File

@ -79,8 +79,6 @@ github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=

View File

@ -1,444 +0,0 @@
package plugin
import (
"context"
"crypto/md5" //nolint:gosec
"errors"
"fmt"
"io"
"mime"
"os"
"path/filepath"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/cloudfront"
cf_types "github.com/aws/aws-sdk-go-v2/service/cloudfront/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
s3_types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/rs/zerolog/log"
"github.com/ryanuber/go-glob"
)
type AWS struct {
client *s3.Client
cfClient *cloudfront.Client
remote []string
local []string
plugin *Plugin
}
func NewAWS(plugin *Plugin) (AWS, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(plugin.Settings.Region),
)
if err != nil {
return AWS{}, fmt.Errorf("error while loading AWS config: %w", err)
}
if plugin.Settings.Endpoint != "" {
cfg.BaseEndpoint = aws.String(plugin.Settings.Endpoint)
}
// allowing to use the instance role or provide a key and secret
if plugin.Settings.AccessKey != "" && plugin.Settings.SecretKey != "" {
cfg.Credentials = credentials.NewStaticCredentialsProvider(plugin.Settings.AccessKey, plugin.Settings.SecretKey, "")
}
c := s3.NewFromConfig(cfg, func(o *s3.Options) {
o.UsePathStyle = plugin.Settings.PathStyle
})
cf := cloudfront.NewFromConfig(cfg)
r := make([]string, 1)
l := make([]string, 1)
return AWS{c, cf, r, l, plugin}, nil
}
//nolint:gocognit,gocyclo,maintidx
func (a *AWS) Upload(local, remote string) error {
plugin := a.plugin
if local == "" {
return nil
}
file, err := os.Open(local)
if err != nil {
return err
}
defer file.Close()
var acl string
for pattern := range plugin.Settings.ACL {
if match := glob.Glob(pattern, local); match {
acl = plugin.Settings.ACL[pattern]
break
}
}
if acl == "" {
acl = "private"
}
fileExt := filepath.Ext(local)
var contentType string
for patternExt := range plugin.Settings.ContentType {
if patternExt == fileExt {
contentType = plugin.Settings.ContentType[patternExt]
break
}
}
if contentType == "" {
contentType = mime.TypeByExtension(fileExt)
}
var contentEncoding string
for patternExt := range plugin.Settings.ContentEncoding {
if patternExt == fileExt {
contentEncoding = plugin.Settings.ContentEncoding[patternExt]
break
}
}
var cacheControl string
for pattern := range plugin.Settings.CacheControl {
if match := glob.Glob(pattern, local); match {
cacheControl = plugin.Settings.CacheControl[pattern]
break
}
}
metadata := map[string]string{}
for pattern := range plugin.Settings.Metadata {
if match := glob.Glob(pattern, local); match {
for k, v := range plugin.Settings.Metadata[pattern] {
metadata[k] = v
}
break
}
}
var noSuchKeyErr *s3_types.NoSuchKey
head, err := a.client.HeadObject(context.TODO(), &s3.HeadObjectInput{
Bucket: aws.String(plugin.Settings.Bucket),
Key: aws.String(remote),
})
if err != nil {
if errors.As(err, &noSuchKeyErr) {
return err
}
log.Debug().Msgf(
"'%s' not found in bucket, uploading with content-type '%s' and permissions '%s'",
local,
contentType,
acl,
)
putObject := &s3.PutObjectInput{
Bucket: aws.String(plugin.Settings.Bucket),
Key: aws.String(remote),
Body: file,
ContentType: aws.String(contentType),
ACL: s3_types.ObjectCannedACL(acl),
Metadata: metadata,
}
if len(cacheControl) > 0 {
putObject.CacheControl = aws.String(cacheControl)
}
if len(contentEncoding) > 0 {
putObject.ContentEncoding = aws.String(contentEncoding)
}
// skip upload during dry run
if a.plugin.Settings.DryRun {
return nil
}
_, err = a.client.PutObject(context.TODO(), putObject)
return err
}
//nolint:gosec
hash := md5.New()
_, _ = io.Copy(hash, file)
sum := fmt.Sprintf("'%x'", hash.Sum(nil))
//nolint:nestif
if sum == *head.ETag {
shouldCopy := false
if head.ContentType == nil && contentType != "" {
log.Debug().Msgf("content-type has changed from unset to %s", contentType)
shouldCopy = true
}
if !shouldCopy && head.ContentType != nil && contentType != *head.ContentType {
log.Debug().Msgf("content-type has changed from %s to %s", *head.ContentType, contentType)
shouldCopy = true
}
if !shouldCopy && head.ContentEncoding == nil && contentEncoding != "" {
log.Debug().Msgf("Content-Encoding has changed from unset to %s", contentEncoding)
shouldCopy = true
}
if !shouldCopy && head.ContentEncoding != nil && contentEncoding != *head.ContentEncoding {
log.Debug().Msgf("Content-Encoding has changed from %s to %s", *head.ContentEncoding, contentEncoding)
shouldCopy = true
}
if !shouldCopy && head.CacheControl == nil && cacheControl != "" {
log.Debug().Msgf("cache-control has changed from unset to %s", cacheControl)
shouldCopy = true
}
if !shouldCopy && head.CacheControl != nil && cacheControl != *head.CacheControl {
log.Debug().Msgf("cache-control has changed from %s to %s", *head.CacheControl, cacheControl)
shouldCopy = true
}
if !shouldCopy && len(head.Metadata) != len(metadata) {
log.Debug().Msgf("count of metadata values has changed for %s", local)
shouldCopy = true
}
if !shouldCopy && len(metadata) > 0 {
for k, v := range metadata {
if hv, ok := head.Metadata[k]; ok {
if v != hv {
log.Debug().Msgf("metadata values have changed for %s", local)
shouldCopy = true
break
}
}
}
}
if !shouldCopy {
grant, err := a.client.GetObjectAcl(context.TODO(), &s3.GetObjectAclInput{
Bucket: aws.String(plugin.Settings.Bucket),
Key: aws.String(remote),
})
if err != nil {
return err
}
previousACL := "private"
for _, grant := range grant.Grants {
grantee := *grant.Grantee
if grantee.URI != nil {
if *grantee.URI == "http://acs.amazonaws.com/groups/global/AllUsers" {
if grant.Permission == "READ" {
previousACL = "public-read"
} else if grant.Permission == "WRITE" {
previousACL = "public-read-write"
}
}
if *grantee.URI == "http://acs.amazonaws.com/groups/global/AuthenticatedUsers" {
if grant.Permission == "READ" {
previousACL = "authenticated-read"
}
}
}
}
if previousACL != acl {
log.Debug().Msgf("permissions for '%s' have changed from '%s' to '%s'", remote, previousACL, acl)
shouldCopy = true
}
}
if !shouldCopy {
log.Debug().Msgf("skipping '%s' because hashes and metadata match", local)
return nil
}
log.Debug().Msgf("updating metadata for '%s' content-type: '%s', ACL: '%s'", local, contentType, acl)
copyObject := &s3.CopyObjectInput{
Bucket: aws.String(plugin.Settings.Bucket),
Key: aws.String(remote),
CopySource: aws.String(fmt.Sprintf("%s/%s", plugin.Settings.Bucket, remote)),
ACL: s3_types.ObjectCannedACL(acl),
ContentType: aws.String(contentType),
Metadata: metadata,
MetadataDirective: s3_types.MetadataDirectiveReplace,
}
if len(cacheControl) > 0 {
copyObject.CacheControl = aws.String(cacheControl)
}
if len(contentEncoding) > 0 {
copyObject.ContentEncoding = aws.String(contentEncoding)
}
// skip update if dry run
if a.plugin.Settings.DryRun {
return nil
}
_, err = a.client.CopyObject(context.Background(), copyObject)
return err
}
_, err = file.Seek(0, 0)
if err != nil {
return err
}
log.Debug().Msgf("uploading '%s' with content-type '%s' and permissions '%s'", local, contentType, acl)
putObject := &s3.PutObjectInput{
Bucket: aws.String(plugin.Settings.Bucket),
Key: aws.String(remote),
Body: file,
ContentType: aws.String(contentType),
ACL: s3_types.ObjectCannedACL(acl),
Metadata: metadata,
}
if len(cacheControl) > 0 {
putObject.CacheControl = aws.String(cacheControl)
}
if len(contentEncoding) > 0 {
putObject.ContentEncoding = aws.String(contentEncoding)
}
// skip upload if dry run
if a.plugin.Settings.DryRun {
return nil
}
_, err = a.client.PutObject(context.TODO(), putObject)
return err
}
func (a *AWS) Redirect(path, location string) error {
plugin := a.plugin
log.Debug().Msgf("adding redirect from '%s' to '%s'", path, location)
if a.plugin.Settings.DryRun {
return nil
}
_, err := a.client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(plugin.Settings.Bucket),
Key: aws.String(path),
ACL: s3_types.ObjectCannedACLPublicRead,
WebsiteRedirectLocation: aws.String(location),
})
return err
}
func (a *AWS) Delete(remote string) error {
plugin := a.plugin
log.Debug().Msgf("removing remote file '%s'", remote)
if a.plugin.Settings.DryRun {
return nil
}
_, err := a.client.DeleteObject(context.TODO(), &s3.DeleteObjectInput{
Bucket: aws.String(plugin.Settings.Bucket),
Key: aws.String(remote),
})
return err
}
func (a *AWS) List(path string) ([]string, error) {
plugin := a.plugin
remote := make([]string, 0)
resp, err := a.client.ListObjects(context.TODO(), &s3.ListObjectsInput{
Bucket: aws.String(plugin.Settings.Bucket),
Prefix: aws.String(path),
})
if err != nil {
return remote, err
}
for _, item := range resp.Contents {
remote = append(remote, *item.Key)
}
for *resp.IsTruncated {
resp, err = a.client.ListObjects(context.TODO(), &s3.ListObjectsInput{
Bucket: aws.String(plugin.Settings.Bucket),
Prefix: aws.String(path),
Marker: aws.String(remote[len(remote)-1]),
})
if err != nil {
return remote, err
}
for _, item := range resp.Contents {
remote = append(remote, *item.Key)
}
}
return remote, nil
}
func (a *AWS) Invalidate(invalidatePath string) error {
p := a.plugin
log.Debug().Msgf("invalidating '%s'", invalidatePath)
_, err := a.cfClient.CreateInvalidation(context.TODO(), &cloudfront.CreateInvalidationInput{
DistributionId: aws.String(p.Settings.CloudFrontDistribution),
InvalidationBatch: &cf_types.InvalidationBatch{
CallerReference: aws.String(time.Now().Format(time.RFC3339Nano)),
Paths: &cf_types.Paths{
Quantity: aws.Int32(1),
Items: []string{
invalidatePath,
},
},
},
})
return err
}

View File

@ -9,6 +9,7 @@ import (
"strings"
"github.com/rs/zerolog/log"
"github.com/thegeeklab/wp-s3-action/aws"
)
var ErrTypeAssertionFailed = errors.New("type assertion failed")
@ -44,9 +45,25 @@ func (p *Plugin) Validate() error {
// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
p.Settings.Jobs = make([]Job, 1)
p.Settings.Client, _ = NewAWS(p)
if err := p.createSyncJobs(); err != nil {
client, err := aws.NewClient(
p.Network.Context,
p.Settings.Endpoint,
p.Settings.Region,
p.Settings.AccessKey,
p.Settings.SecretKey,
p.Settings.PathStyle,
)
if err != nil {
return fmt.Errorf("error while creating AWS client: %w", err)
}
client.S3.Bucket = p.Settings.Bucket
client.S3.DryRun = p.Settings.DryRun
client.Cloudfront.Distribution = p.Settings.CloudFrontDistribution
if err := p.createSyncJobs(p.Network.Context, client); err != nil {
return fmt.Errorf("error while creating sync job: %w", err)
}
@ -58,15 +75,15 @@ func (p *Plugin) Execute() error {
})
}
if err := p.runJobs(); err != nil {
if err := p.runJobs(p.Network.Context, client); err != nil {
return fmt.Errorf("error while creating sync job: %w", err)
}
return nil
}
func (p *Plugin) createSyncJobs() error {
remote, err := p.Settings.Client.List(p.Settings.Target)
func (p *Plugin) createSyncJobs(ctx context.Context, client *aws.Client) error {
remote, err := client.S3.List(ctx, aws.S3ListOpt{Path: p.Settings.Target})
if err != nil {
return err
}
@ -134,8 +151,7 @@ func (p *Plugin) createSyncJobs() error {
return nil
}
func (p *Plugin) runJobs() error {
client := p.Settings.Client
func (p *Plugin) runJobs(ctx context.Context, client *aws.Client) error {
jobChan := make(chan struct{}, p.Settings.MaxConcurrency)
results := make(chan *Result, len(p.Settings.Jobs))
@ -151,11 +167,27 @@ func (p *Plugin) runJobs() error {
switch job.action {
case "upload":
err = client.Upload(job.local, job.remote)
opt := aws.S3UploadOpt{
LocalFilePath: job.local,
RemoteObjectKey: job.remote,
ACL: p.Settings.ACL,
ContentType: p.Settings.ContentType,
ContentEncoding: p.Settings.ContentEncoding,
CacheControl: p.Settings.CacheControl,
Metadata: p.Settings.Metadata,
}
err = client.S3.Upload(ctx, opt)
case "redirect":
err = client.Redirect(job.local, job.remote)
opt := aws.S3RedirectOpt{
Path: job.local,
Location: job.remote,
}
err = client.S3.Redirect(ctx, opt)
case "delete":
err = client.Delete(job.remote)
opt := aws.S3DeleteOpt{
RemoteObjectKey: job.remote,
}
err = client.S3.Delete(ctx, opt)
case "invalidateCloudFront":
invalidateJob = &job
default:
@ -175,7 +207,11 @@ func (p *Plugin) runJobs() error {
}
if invalidateJob != nil {
err := client.Invalidate(invalidateJob.remote)
opt := aws.CloudfrontInvalidateOpt{
Path: invalidateJob.remote,
}
err := client.Cloudfront.Invalidate(ctx, opt)
if err != nil {
return fmt.Errorf("failed to %s %s to %s: %w", invalidateJob.action, invalidateJob.local, invalidateJob.remote, err)
}

View File

@ -35,7 +35,6 @@ type Settings struct {
CloudFrontDistribution string
DryRun bool
PathStyle bool
Client AWS
Jobs []Job
MaxConcurrency int
}