github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/obs/client_object.go (about) 1 package obs 2 3 import ( 4 "errors" 5 "io" 6 "os" 7 "strings" 8 ) 9 10 // ListObjects lists objects in a bucket. 11 // 12 // You can use this API to list objects in a bucket. By default, a maximum of 1000 objects are listed. 13 func (obsClient ObsClient) ListObjects(input *ListObjectsInput) (output *ListObjectsOutput, err error) { 14 if input == nil { 15 return nil, errors.New("ListObjectsInput is nil") 16 } 17 output = &ListObjectsOutput{} 18 err = obsClient.doActionWithBucket("ListObjects", HTTP_GET, input.Bucket, input, output) 19 if err != nil { 20 output = nil 21 } else { 22 if location, ok := output.ResponseHeaders[HEADER_BUCKET_REGION]; ok { 23 output.Location = location[0] 24 } 25 } 26 return 27 } 28 29 // ListVersions lists versioning objects in a bucket. 30 // 31 // You can use this API to list versioning objects in a bucket. By default, a maximum of 1000 versioning objects are listed. 32 func (obsClient ObsClient) ListVersions(input *ListVersionsInput) (output *ListVersionsOutput, err error) { 33 if input == nil { 34 return nil, errors.New("ListVersionsInput is nil") 35 } 36 output = &ListVersionsOutput{} 37 err = obsClient.doActionWithBucket("ListVersions", HTTP_GET, input.Bucket, input, output) 38 if err != nil { 39 output = nil 40 } else { 41 if location, ok := output.ResponseHeaders[HEADER_BUCKET_REGION]; ok { 42 output.Location = location[0] 43 } 44 } 45 return 46 } 47 48 // HeadObject checks whether an object exists. 49 // 50 // You can use this API to check whether an object exists. 51 func (obsClient ObsClient) HeadObject(input *HeadObjectInput) (output *BaseModel, err error) { 52 if input == nil { 53 return nil, errors.New("HeadObjectInput is nil") 54 } 55 output = &BaseModel{} 56 err = obsClient.doActionWithBucketAndKey("HeadObject", HTTP_HEAD, input.Bucket, input.Key, input, output) 57 if err != nil { 58 output = nil 59 } 60 return 61 } 62 63 // SetObjectMetadata sets object metadata. 64 func (obsClient ObsClient) SetObjectMetadata(input *SetObjectMetadataInput) (output *SetObjectMetadataOutput, err error) { 65 output = &SetObjectMetadataOutput{} 66 err = obsClient.doActionWithBucketAndKey("SetObjectMetadata", HTTP_PUT, input.Bucket, input.Key, input, output) 67 if err != nil { 68 output = nil 69 } else { 70 ParseSetObjectMetadataOutput(output) 71 } 72 return 73 } 74 75 // DeleteObject deletes an object. 76 // 77 // You can use this API to delete an object from a specified bucket. 78 func (obsClient ObsClient) DeleteObject(input *DeleteObjectInput) (output *DeleteObjectOutput, err error) { 79 if input == nil { 80 return nil, errors.New("DeleteObjectInput is nil") 81 } 82 output = &DeleteObjectOutput{} 83 err = obsClient.doActionWithBucketAndKey("DeleteObject", HTTP_DELETE, input.Bucket, input.Key, input, output) 84 if err != nil { 85 output = nil 86 } else { 87 ParseDeleteObjectOutput(output) 88 } 89 return 90 } 91 92 // DeleteObjects deletes objects in a batch. 93 // 94 // You can use this API to batch delete objects from a specified bucket. 95 func (obsClient ObsClient) DeleteObjects(input *DeleteObjectsInput) (output *DeleteObjectsOutput, err error) { 96 if input == nil { 97 return nil, errors.New("DeleteObjectsInput is nil") 98 } 99 output = &DeleteObjectsOutput{} 100 err = obsClient.doActionWithBucket("DeleteObjects", HTTP_POST, input.Bucket, input, output) 101 if err != nil { 102 output = nil 103 } 104 return 105 } 106 107 // SetObjectAcl sets ACL for an object. 108 // 109 // You can use this API to set the ACL for an object in a specified bucket. 110 func (obsClient ObsClient) SetObjectAcl(input *SetObjectAclInput) (output *BaseModel, err error) { 111 if input == nil { 112 return nil, errors.New("SetObjectAclInput is nil") 113 } 114 output = &BaseModel{} 115 err = obsClient.doActionWithBucketAndKey("SetObjectAcl", HTTP_PUT, input.Bucket, input.Key, input, output) 116 if err != nil { 117 output = nil 118 } 119 return 120 } 121 122 // GetObjectAcl gets the ACL of an object. 123 // 124 // You can use this API to obtain the ACL of an object in a specified bucket. 125 func (obsClient ObsClient) GetObjectAcl(input *GetObjectAclInput) (output *GetObjectAclOutput, err error) { 126 if input == nil { 127 return nil, errors.New("GetObjectAclInput is nil") 128 } 129 output = &GetObjectAclOutput{} 130 err = obsClient.doActionWithBucketAndKey("GetObjectAcl", HTTP_GET, input.Bucket, input.Key, input, output) 131 if err != nil { 132 output = nil 133 } else { 134 if versionId, ok := output.ResponseHeaders[HEADER_VERSION_ID]; ok { 135 output.VersionId = versionId[0] 136 } 137 } 138 return 139 } 140 141 // RestoreObject restores an object. 142 func (obsClient ObsClient) RestoreObject(input *RestoreObjectInput) (output *BaseModel, err error) { 143 if input == nil { 144 return nil, errors.New("RestoreObjectInput is nil") 145 } 146 output = &BaseModel{} 147 err = obsClient.doActionWithBucketAndKey("RestoreObject", HTTP_POST, input.Bucket, input.Key, input, output) 148 if err != nil { 149 output = nil 150 } 151 return 152 } 153 154 // GetObjectMetadata gets object metadata. 155 // 156 // You can use this API to send a HEAD request to the object of a specified bucket to obtain its metadata. 157 func (obsClient ObsClient) GetObjectMetadata(input *GetObjectMetadataInput) (output *GetObjectMetadataOutput, err error) { 158 if input == nil { 159 return nil, errors.New("GetObjectMetadataInput is nil") 160 } 161 output = &GetObjectMetadataOutput{} 162 err = obsClient.doActionWithBucketAndKey("GetObjectMetadata", HTTP_HEAD, input.Bucket, input.Key, input, output) 163 if err != nil { 164 output = nil 165 } else { 166 ParseGetObjectMetadataOutput(output) 167 } 168 return 169 } 170 171 // GetObject downloads object. 172 // 173 // You can use this API to download an object in a specified bucket. 174 func (obsClient ObsClient) GetObject(input *GetObjectInput) (output *GetObjectOutput, err error) { 175 if input == nil { 176 return nil, errors.New("GetObjectInput is nil") 177 } 178 output = &GetObjectOutput{} 179 err = obsClient.doActionWithBucketAndKey("GetObject", HTTP_GET, input.Bucket, input.Key, input, output) 180 if err != nil { 181 output = nil 182 } else { 183 ParseGetObjectOutput(output) 184 } 185 return 186 } 187 188 // PutObject uploads an object to the specified bucket. 189 func (obsClient ObsClient) PutObject(input *PutObjectInput) (output *PutObjectOutput, err error) { 190 if input == nil { 191 return nil, errors.New("PutObjectInput is nil") 192 } 193 194 if input.ContentType == "" && input.Key != "" { 195 if contentType, ok := mimeTypes[strings.ToLower(input.Key[strings.LastIndex(input.Key, ".")+1:])]; ok { 196 input.ContentType = contentType 197 } 198 } 199 200 output = &PutObjectOutput{} 201 var repeatable bool 202 if input.Body != nil { 203 _, repeatable = input.Body.(*strings.Reader) 204 if input.ContentLength > 0 { 205 input.Body = &readerWrapper{reader: input.Body, totalCount: input.ContentLength} 206 } 207 } 208 if repeatable { 209 err = obsClient.doActionWithBucketAndKey("PutObject", HTTP_PUT, input.Bucket, input.Key, input, output) 210 } else { 211 err = obsClient.doActionWithBucketAndKeyUnRepeatable("PutObject", HTTP_PUT, input.Bucket, input.Key, input, output) 212 } 213 if err != nil { 214 output = nil 215 } else { 216 ParsePutObjectOutput(output) 217 } 218 return 219 } 220 221 func (obsClient ObsClient) getContentType(input *PutObjectInput, sourceFile string) (contentType string) { 222 if contentType, ok := mimeTypes[strings.ToLower(input.Key[strings.LastIndex(input.Key, ".")+1:])]; ok { 223 return contentType 224 } 225 if contentType, ok := mimeTypes[strings.ToLower(sourceFile[strings.LastIndex(sourceFile, ".")+1:])]; ok { 226 return contentType 227 } 228 return 229 } 230 231 func (obsClient ObsClient) isGetContentType(input *PutObjectInput) bool { 232 if input.ContentType == "" && input.Key != "" { 233 return true 234 } 235 return false 236 } 237 238 // PutFile uploads a file to the specified bucket. 239 func (obsClient ObsClient) PutFile(input *PutFileInput) (output *PutObjectOutput, err error) { 240 if input == nil { 241 return nil, errors.New("PutFileInput is nil") 242 } 243 244 var body io.Reader 245 sourceFile := strings.TrimSpace(input.SourceFile) 246 if sourceFile != "" { 247 fd, _err := os.Open(sourceFile) 248 if _err != nil { 249 err = _err 250 return nil, err 251 } 252 defer func() { 253 errMsg := fd.Close() 254 if errMsg != nil { 255 doLog(LEVEL_WARN, "Failed to close file with reason: %v", errMsg) 256 } 257 }() 258 259 stat, _err := fd.Stat() 260 if _err != nil { 261 err = _err 262 return nil, err 263 } 264 fileReaderWrapper := &fileReaderWrapper{filePath: sourceFile} 265 fileReaderWrapper.reader = fd 266 if input.ContentLength > 0 { 267 if input.ContentLength > stat.Size() { 268 input.ContentLength = stat.Size() 269 } 270 fileReaderWrapper.totalCount = input.ContentLength 271 } else { 272 fileReaderWrapper.totalCount = stat.Size() 273 } 274 body = fileReaderWrapper 275 } 276 277 _input := &PutObjectInput{} 278 _input.PutObjectBasicInput = input.PutObjectBasicInput 279 _input.Body = body 280 281 if obsClient.isGetContentType(_input) { 282 _input.ContentType = obsClient.getContentType(_input, sourceFile) 283 } 284 285 output = &PutObjectOutput{} 286 err = obsClient.doActionWithBucketAndKey("PutFile", HTTP_PUT, _input.Bucket, _input.Key, _input, output) 287 if err != nil { 288 output = nil 289 } else { 290 ParsePutObjectOutput(output) 291 } 292 return 293 } 294 295 // CopyObject creates a copy for an existing object. 296 // 297 // You can use this API to create a copy for an object in a specified bucket. 298 func (obsClient ObsClient) CopyObject(input *CopyObjectInput) (output *CopyObjectOutput, err error) { 299 if input == nil { 300 return nil, errors.New("CopyObjectInput is nil") 301 } 302 303 if strings.TrimSpace(input.CopySourceBucket) == "" { 304 return nil, errors.New("Source bucket is empty") 305 } 306 if strings.TrimSpace(input.CopySourceKey) == "" { 307 return nil, errors.New("Source key is empty") 308 } 309 310 output = &CopyObjectOutput{} 311 err = obsClient.doActionWithBucketAndKey("CopyObject", HTTP_PUT, input.Bucket, input.Key, input, output) 312 if err != nil { 313 output = nil 314 } else { 315 ParseCopyObjectOutput(output) 316 } 317 return 318 }