Last active
June 13, 2019 20:40
-
-
Save mccurdyc/918980e66bfc51c8b171751e050d760e to your computer and use it in GitHub Desktop.
S3 Mock Upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package output | |
import ( | |
"bytes" | |
"github.com/aws/aws-sdk-go/service/s3/s3iface" | |
"github.com/aws/aws-sdk-go/service/s3/s3manager" | |
) | |
type S3Writer struct { | |
s3iface.S3API | |
Uploader *s3manager.Uploader | |
UploadInput *s3manager.UploadInput | |
} | |
func (s *S3Writer) write(b []byte) error { | |
s.UploadInput.Body = bytes.NewReader(b) | |
_, err := s.Uploader.Upload(s.UploadInput) | |
return err | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package output | |
import ( | |
"crypto/tls" | |
"io" | |
"mime/multipart" | |
"net/http" | |
"net/url" | |
"testing" | |
"time" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/client/metadata" | |
"github.com/aws/aws-sdk-go/aws/credentials" | |
"github.com/aws/aws-sdk-go/aws/request" | |
"github.com/aws/aws-sdk-go/service/s3" | |
"github.com/aws/aws-sdk-go/service/s3/s3iface" | |
"github.com/aws/aws-sdk-go/service/s3/s3manager" | |
"github.com/stretchr/testify/assert" | |
) | |
// https://github.com/aws/aws-sdk-go-v2/blob/master/example/service/s3/mockPaginator/mockPaginator_test.go | |
type mockS3Client struct { | |
s3iface.S3API | |
uploadErr error | |
body []byte | |
} | |
func (m *mockS3Client) PutObjectRequest(*s3.PutObjectInput) (*request.Request, *s3.PutObjectOutput) { | |
return &request.Request{ | |
Config: aws.Config{ | |
CredentialsChainVerboseErrors: nil, | |
Credentials: &credentials.Credentials{}, | |
Endpoint: nil, | |
EndpointResolver: nil, | |
EnforceShouldRetryCheck: nil, | |
Region: nil, | |
DisableSSL: nil, | |
HTTPClient: &http.Client{ | |
Transport: nil, | |
CheckRedirect: func(*http.Request, []*http.Request) error { return nil }, | |
Jar: nil, | |
Timeout: 0, | |
}, | |
LogLevel: nil, | |
Logger: nil, | |
MaxRetries: nil, | |
Retryer: nil, | |
DisableParamValidation: nil, | |
DisableComputeChecksums: nil, | |
S3ForcePathStyle: nil, | |
S3Disable100Continue: nil, | |
S3UseAccelerate: nil, | |
S3DisableContentMD5Validation: nil, | |
EC2MetadataDisableTimeoutOverride: nil, | |
UseDualStack: nil, | |
SleepDelay: func(time.Duration) { return }, | |
DisableRestProtocolURICleaning: nil, | |
EnableEndpointDiscovery: nil, | |
DisableEndpointHostPrefix: nil, | |
}, | |
ClientInfo: metadata.ClientInfo{ | |
ServiceName: "", | |
ServiceID: "", | |
APIVersion: "", | |
Endpoint: "", | |
SigningName: "", | |
SigningRegion: "", | |
JSONVersion: "", | |
TargetPrefix: "", | |
}, | |
Handlers: request.Handlers{ | |
Validate: request.HandlerList{ | |
AfterEachFn: func(request.HandlerListRunItem) bool { return true }, | |
}, | |
Build: request.HandlerList{ | |
AfterEachFn: func(request.HandlerListRunItem) bool { return true }, | |
}, | |
Sign: request.HandlerList{ | |
AfterEachFn: func(request.HandlerListRunItem) bool { return true }, | |
}, | |
Send: request.HandlerList{ | |
AfterEachFn: func(request.HandlerListRunItem) bool { return true }, | |
}, | |
ValidateResponse: request.HandlerList{ | |
AfterEachFn: func(request.HandlerListRunItem) bool { return true }, | |
}, | |
Unmarshal: request.HandlerList{ | |
AfterEachFn: func(request.HandlerListRunItem) bool { return true }, | |
}, | |
UnmarshalStream: request.HandlerList{ | |
AfterEachFn: func(request.HandlerListRunItem) bool { return true }, | |
}, | |
UnmarshalMeta: request.HandlerList{ | |
AfterEachFn: func(request.HandlerListRunItem) bool { return true }, | |
}, | |
UnmarshalError: request.HandlerList{ | |
AfterEachFn: func(request.HandlerListRunItem) bool { return true }, | |
}, | |
Retry: request.HandlerList{ | |
AfterEachFn: func(request.HandlerListRunItem) bool { return true }, | |
}, | |
AfterRetry: request.HandlerList{ | |
AfterEachFn: func(request.HandlerListRunItem) bool { return true }, | |
}, | |
CompleteAttempt: request.HandlerList{ | |
AfterEachFn: func(request.HandlerListRunItem) bool { return true }, | |
}, | |
Complete: request.HandlerList{ | |
AfterEachFn: func(request.HandlerListRunItem) bool { return true }, | |
}, | |
}, | |
Retryer: nil, | |
AttemptTime: time.Time{}, | |
Time: time.Time{}, | |
Operation: &request.Operation{ | |
Name: "", | |
HTTPMethod: "", | |
HTTPPath: "", | |
Paginator: &request.Paginator{ | |
InputTokens: nil, | |
OutputTokens: nil, | |
LimitToken: "", | |
TruncationToken: "", | |
}, | |
BeforePresignFn: func(*request.Request) error { return nil }, | |
}, | |
HTTPRequest: &http.Request{ | |
Method: "", | |
URL: &url.URL{ | |
Scheme: "", | |
Opaque: "", | |
User: &url.Userinfo{}, | |
Host: "", | |
Path: "", | |
RawPath: "", | |
ForceQuery: false, | |
RawQuery: "", | |
Fragment: "", | |
}, | |
Proto: "", | |
ProtoMajor: 0, | |
ProtoMinor: 0, | |
Header: map[string][]string{ | |
"": nil, | |
}, | |
Body: nil, | |
GetBody: func() (io.ReadCloser, error) { return nil, nil }, | |
ContentLength: 0, | |
TransferEncoding: nil, | |
Close: false, | |
Host: "", | |
Form: map[string][]string{ | |
"": nil, | |
}, | |
PostForm: map[string][]string{ | |
"": nil, | |
}, | |
MultipartForm: &multipart.Form{ | |
Value: map[string][]string{ | |
"": nil, | |
}, | |
File: map[string][]*multipart.FileHeader{ | |
"": nil, | |
}, | |
}, | |
Trailer: map[string][]string{ | |
"": nil, | |
}, | |
RemoteAddr: "", | |
RequestURI: "", | |
TLS: &tls.ConnectionState{ | |
Version: 0, | |
HandshakeComplete: false, | |
DidResume: false, | |
CipherSuite: 0, | |
NegotiatedProtocol: "", | |
NegotiatedProtocolIsMutual: false, | |
ServerName: "", | |
PeerCertificates: nil, | |
VerifiedChains: nil, | |
SignedCertificateTimestamps: nil, | |
OCSPResponse: nil, | |
TLSUnique: nil, | |
}, | |
Cancel: make(<-chan struct{}), | |
Response: &http.Response{ | |
Status: "", | |
StatusCode: 0, | |
Proto: "", | |
ProtoMajor: 0, | |
ProtoMinor: 0, | |
Header: map[string][]string{ | |
"": nil, | |
}, | |
Body: nil, | |
ContentLength: 0, | |
TransferEncoding: nil, | |
Close: false, | |
Uncompressed: false, | |
Trailer: map[string][]string{ | |
"": nil, | |
}, | |
Request: &http.Request{}, | |
TLS: &tls.ConnectionState{ | |
Version: 0, | |
HandshakeComplete: false, | |
DidResume: false, | |
CipherSuite: 0, | |
NegotiatedProtocol: "", | |
NegotiatedProtocolIsMutual: false, | |
ServerName: "", | |
PeerCertificates: nil, | |
VerifiedChains: nil, | |
SignedCertificateTimestamps: nil, | |
OCSPResponse: nil, | |
TLSUnique: nil, | |
}, | |
}, | |
}, | |
HTTPResponse: &http.Response{ | |
Status: "", | |
StatusCode: 0, | |
Proto: "", | |
ProtoMajor: 0, | |
ProtoMinor: 0, | |
Header: map[string][]string{ | |
"": nil, | |
}, | |
Body: nil, | |
ContentLength: 0, | |
TransferEncoding: nil, | |
Close: false, | |
Uncompressed: false, | |
Trailer: map[string][]string{ | |
"": nil, | |
}, | |
Request: &http.Request{ | |
Method: "", | |
URL: &url.URL{ | |
Scheme: "", | |
Opaque: "", | |
User: &url.Userinfo{}, | |
Host: "", | |
Path: "", | |
RawPath: "", | |
ForceQuery: false, | |
RawQuery: "", | |
Fragment: "", | |
}, | |
Proto: "", | |
ProtoMajor: 0, | |
ProtoMinor: 0, | |
Header: map[string][]string{ | |
"": nil, | |
}, | |
Body: nil, | |
GetBody: func() (io.ReadCloser, error) { return nil, nil }, | |
ContentLength: 0, | |
TransferEncoding: nil, | |
Close: false, | |
Host: "", | |
Form: map[string][]string{ | |
"": nil, | |
}, | |
PostForm: map[string][]string{ | |
"": nil, | |
}, | |
MultipartForm: &multipart.Form{ | |
Value: map[string][]string{ | |
"": nil, | |
}, | |
File: map[string][]*multipart.FileHeader{ | |
"": nil, | |
}, | |
}, | |
Trailer: map[string][]string{ | |
"": nil, | |
}, | |
RemoteAddr: "", | |
RequestURI: "", | |
TLS: &tls.ConnectionState{ | |
Version: 0, | |
HandshakeComplete: false, | |
DidResume: false, | |
CipherSuite: 0, | |
NegotiatedProtocol: "", | |
NegotiatedProtocolIsMutual: false, | |
ServerName: "", | |
PeerCertificates: nil, | |
VerifiedChains: nil, | |
SignedCertificateTimestamps: nil, | |
OCSPResponse: nil, | |
TLSUnique: nil, | |
}, | |
Cancel: make(<-chan struct{}), | |
Response: &http.Response{}, | |
}, | |
TLS: &tls.ConnectionState{ | |
Version: 0, | |
HandshakeComplete: false, | |
DidResume: false, | |
CipherSuite: 0, | |
NegotiatedProtocol: "", | |
NegotiatedProtocolIsMutual: false, | |
ServerName: "", | |
PeerCertificates: nil, | |
VerifiedChains: nil, | |
SignedCertificateTimestamps: nil, | |
OCSPResponse: nil, | |
TLSUnique: nil, | |
}, | |
}, | |
Body: nil, | |
BodyStart: 0, | |
Params: nil, | |
Error: nil, | |
Data: nil, | |
RequestID: "", | |
RetryCount: 0, | |
Retryable: nil, | |
RetryDelay: 0, | |
NotHoist: false, | |
SignedHeaderVals: map[string][]string{ | |
"": nil, | |
}, | |
LastSignedAt: time.Time{}, | |
DisableFollowRedirects: false, | |
ExpireTime: 0, | |
}, &s3.PutObjectOutput{ | |
ETag: aws.String("etag"), | |
Expiration: aws.String("expiration"), | |
RequestCharged: aws.String("requestcharged"), | |
SSECustomerAlgorithm: aws.String("ssecustomeralg"), | |
SSECustomerKeyMD5: aws.String("sscustomerkeymd5"), | |
SSEKMSKeyId: aws.String("ssekmskeyid"), | |
ServerSideEncryption: aws.String("sse"), | |
VersionId: aws.String("version_id"), | |
} | |
} | |
func TestS3Write(t *testing.T) { | |
svc := &mockS3Client{ | |
uploadErr: nil, | |
body: []byte{'h', 'e', 'l', 'l', 'o'}, | |
} | |
s3writer := &S3Writer{ | |
Uploader: s3manager.NewUploaderWithClient(svc), | |
UploadInput: &s3manager.UploadInput{ | |
Bucket: aws.String("bucket"), | |
Key: aws.String("key"), | |
}, | |
} | |
err := s3writer.write([]byte{'h', 'e', 'l', 'l', 'o'}) | |
assert.Nil(t, err, "Expected no error") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package output | |
type Writer interface { | |
write([]byte) error | |
} | |
func Write(b []byte, w Writer) error { | |
return w.write(b) | |
} |
before you copy this, this test does nothing.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
References: