s3/client_op_object_head.go   B
last analyzed

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 45
eloc 151
dl 0
loc 221
rs 8.8
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A s3.*S3.HeadObject 0 17 4
D s3.HeadObjectRequest.ToInput 0 43 13
F s3.NewHeadObjectResult 0 90 28
1
package s3
2
3
import (
4
	"context"
5
	"time"
6
7
	"github.com/aws/aws-sdk-go-v2/aws/awserr"
8
	SDK "github.com/aws/aws-sdk-go-v2/service/s3"
9
10
	"github.com/evalphobia/aws-sdk-go-v2-wrapper/errors"
11
	"github.com/evalphobia/aws-sdk-go-v2-wrapper/private/pointers"
12
)
13
14
// HeadObject executes `HeadObject` operation.
15
func (svc *S3) HeadObject(ctx context.Context, r HeadObjectRequest) (*HeadObjectResult, error) {
16
	out, err := svc.RawHeadObject(ctx, r.ToInput())
17
	if err == nil {
18
		return NewHeadObjectResult(out), nil
19
	}
20
	if aerr, ok := err.(awserr.Error); ok {
21
		if aerr.Code() == "NotFound" {
22
			return NewHeadObjectResult(out), nil
23
		}
24
	}
25
26
	err = svc.errWrap(errors.ErrorData{
27
		Err:          err,
28
		AWSOperation: "HeadObject",
29
	})
30
	svc.Errorf(err.Error())
0 ignored issues
show
introduced by
can't check non-constant format in call to Errorf
Loading history...
31
	return nil, err
32
}
33
34
// HeadObjectRequest has parameters for `HeadObject` operation.
35
type HeadObjectRequest struct {
36
	Bucket string
37
	Key    string
38
39
	// optional
40
	IfMatch              string
41
	IfModifiedSince      time.Time
42
	IfNoneMatch          string
43
	IfUnmodifiedSince    time.Time
44
	PartNumber           int64
45
	Range                string
46
	RequestPayer         RequestPayer
47
	SSECustomerAlgorithm string
48
	SSECustomerKey       string
49
	SSECustomerKeyMD5    string
50
	VersionID            string
51
}
52
53
func (r HeadObjectRequest) ToInput() *SDK.HeadObjectInput {
0 ignored issues
show
introduced by
exported method HeadObjectRequest.ToInput should have comment or be unexported
Loading history...
54
	in := &SDK.HeadObjectInput{}
55
	if r.Bucket != "" {
56
		in.Bucket = pointers.String(r.Bucket)
57
	}
58
	if r.Key != "" {
59
		in.Key = pointers.String(r.Key)
60
	}
61
62
	if r.IfMatch != "" {
63
		in.IfMatch = pointers.String(r.IfMatch)
64
	}
65
	if !r.IfModifiedSince.IsZero() {
66
		in.IfModifiedSince = &r.IfModifiedSince
67
	}
68
	if r.IfNoneMatch != "" {
69
		in.IfNoneMatch = pointers.String(r.IfNoneMatch)
70
	}
71
	if !r.IfUnmodifiedSince.IsZero() {
72
		in.IfUnmodifiedSince = &r.IfUnmodifiedSince
73
	}
74
	if r.PartNumber != 0 {
75
		in.PartNumber = pointers.Long64(r.PartNumber)
76
	}
77
	if r.Range != "" {
78
		in.Range = pointers.String(r.Range)
79
	}
80
81
	in.RequestPayer = SDK.RequestPayer(r.RequestPayer)
82
83
	if r.SSECustomerAlgorithm != "" {
84
		in.SSECustomerAlgorithm = pointers.String(r.SSECustomerAlgorithm)
85
	}
86
	if r.SSECustomerKey != "" {
87
		in.SSECustomerKey = pointers.String(r.SSECustomerKey)
88
	}
89
	if r.SSECustomerKeyMD5 != "" {
90
		in.SSECustomerKeyMD5 = pointers.String(r.SSECustomerKeyMD5)
91
	}
92
	if r.VersionID != "" {
93
		in.VersionId = pointers.String(r.VersionID)
94
	}
95
	return in
96
}
97
98
// HeadObjectResult contains results from `HeadObject` operation.
99
type HeadObjectResult struct {
100
	Exists bool
101
102
	AcceptRanges              string
103
	CacheControl              string
104
	ContentDisposition        string
105
	ContentEncoding           string
106
	ContentLanguage           string
107
	ContentLength             int64
108
	ContentType               string
109
	DeleteMarker              bool
110
	ETag                      string
111
	Expiration                string
112
	Expires                   string
113
	LastModified              time.Time
114
	Metadata                  map[string]string
115
	MissingMeta               int64
116
	ObjectLockLegalHoldStatus ObjectLockLegalHoldStatus
117
	ObjectLockMode            ObjectLockMode
118
	ObjectLockRetainUntilDate time.Time
119
	PartsCount                int64
120
	ReplicationStatus         ReplicationStatus
121
	RequestCharged            RequestCharged
122
	Restore                   string
123
	SSECustomerAlgorithm      string
124
	SSECustomerKeyMD5         string
125
	SSEKMSKeyID               string
126
	ServerSideEncryption      ServerSideEncryption
127
	StorageClass              StorageClass
128
	VersionID                 string
129
	WebsiteRedirectLocation   string
130
}
131
132
func NewHeadObjectResult(output *SDK.HeadObjectResponse) *HeadObjectResult {
0 ignored issues
show
introduced by
exported function NewHeadObjectResult should have comment or be unexported
Loading history...
133
	r := &HeadObjectResult{}
134
	if output == nil {
135
		return r
136
	}
137
138
	r.Exists = true
139
140
	if output.AcceptRanges != nil {
141
		r.AcceptRanges = *output.AcceptRanges
142
	}
143
	if output.CacheControl != nil {
144
		r.CacheControl = *output.CacheControl
145
	}
146
	if output.ContentDisposition != nil {
147
		r.ContentDisposition = *output.ContentDisposition
148
	}
149
	if output.ContentEncoding != nil {
150
		r.ContentEncoding = *output.ContentEncoding
151
	}
152
	if output.ContentLanguage != nil {
153
		r.ContentLanguage = *output.ContentLanguage
154
	}
155
	if output.ContentLength != nil {
156
		r.ContentLength = *output.ContentLength
157
	}
158
	if output.ContentType != nil {
159
		r.ContentType = *output.ContentType
160
	}
161
	if output.DeleteMarker != nil {
162
		r.DeleteMarker = *output.DeleteMarker
163
	}
164
	if output.ETag != nil {
165
		r.ETag = *output.ETag
166
	}
167
	if output.Expiration != nil {
168
		r.Expiration = *output.Expiration
169
	}
170
	if output.Expires != nil {
171
		r.Expires = *output.Expires
172
	}
173
	if output.LastModified != nil {
174
		r.LastModified = *output.LastModified
175
	}
176
177
	r.Metadata = output.Metadata
178
179
	if output.MissingMeta != nil {
180
		r.MissingMeta = *output.MissingMeta
181
	}
182
	if r.ObjectLockLegalHoldStatus != "" {
183
		r.ObjectLockLegalHoldStatus = ObjectLockLegalHoldStatus(output.ObjectLockLegalHoldStatus)
184
	}
185
	if r.ObjectLockMode != "" {
186
		r.ObjectLockMode = ObjectLockMode(output.ObjectLockMode)
187
	}
188
	if output.PartsCount != nil {
189
		r.PartsCount = *output.PartsCount
190
	}
191
	if r.ReplicationStatus != "" {
192
		r.ReplicationStatus = ReplicationStatus(output.ReplicationStatus)
193
	}
194
	if r.RequestCharged != "" {
195
		r.RequestCharged = RequestCharged(output.RequestCharged)
196
	}
197
	if output.Restore != nil {
198
		r.Restore = *output.Restore
199
	}
200
	if output.SSECustomerAlgorithm != nil {
201
		r.SSECustomerAlgorithm = *output.SSECustomerAlgorithm
202
	}
203
	if output.SSECustomerKeyMD5 != nil {
204
		r.SSECustomerKeyMD5 = *output.SSECustomerKeyMD5
205
	}
206
	if output.SSEKMSKeyId != nil {
207
		r.SSEKMSKeyID = *output.SSEKMSKeyId
208
	}
209
	if r.ServerSideEncryption != "" {
210
		r.ServerSideEncryption = ServerSideEncryption(output.ServerSideEncryption)
211
	}
212
	if r.StorageClass != "" {
213
		r.StorageClass = StorageClass(output.StorageClass)
214
	}
215
	if output.VersionId != nil {
216
		r.VersionID = *output.VersionId
217
	}
218
	if output.WebsiteRedirectLocation != nil {
219
		r.WebsiteRedirectLocation = *output.WebsiteRedirectLocation
220
	}
221
	return r
222
}
223