OSSClient.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. //
  2. // OSSClient.h
  3. // oss_ios_sdk
  4. //
  5. // Created by zhouzhuo on 8/16/15.
  6. // Copyright (c) 2015 aliyun.com. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @class OSSGetServiceRequest;
  10. @class OSSCreateBucketRequest;
  11. @class OSSDeleteBucketRequest;
  12. @class OSSHeadObjectRequest;
  13. @class OSSGetBucketRequest;
  14. @class OSSGetBucketACLRequest;
  15. @class OSSGetObjectRequest;
  16. @class OSSGetObjectACLRequest;
  17. @class OSSPutObjectRequest;
  18. @class OSSPutObjectACLRequest;
  19. @class OSSDeleteObjectRequest;
  20. @class OSSDeleteMultipleObjectsRequest;
  21. @class OSSCopyObjectRequest;
  22. @class OSSInitMultipartUploadRequest;
  23. @class OSSUploadPartRequest;
  24. @class OSSCompleteMultipartUploadRequest;
  25. @class OSSListPartsRequest;
  26. @class OSSListMultipartUploadsRequest;
  27. @class OSSAbortMultipartUploadRequest;
  28. @class OSSAppendObjectRequest;
  29. @class OSSResumableUploadRequest;
  30. @class OSSMultipartUploadRequest;
  31. @class OSSCallBackRequest;
  32. @class OSSImagePersistRequest;
  33. @class OSSGetBucketInfoRequest;
  34. @class OSSPutSymlinkRequest;
  35. @class OSSGetSymlinkRequest;
  36. @class OSSRestoreObjectRequest;
  37. @class OSSGetObjectTaggingRequest;
  38. @class OSSDeleteObjectTaggingRequest;
  39. @class OSSPutObjectTaggingRequest;
  40. @class OSSTask;
  41. @class OSSExecutor;
  42. @class OSSNetworking;
  43. @class OSSClientConfiguration;
  44. @protocol OSSCredentialProvider;
  45. NS_ASSUME_NONNULL_BEGIN
  46. /**
  47. OSSClient is the entry class to access OSS in an iOS client. It provides all the methods to communicate with OSS.
  48. Generally speaking, only one instance of OSSClient is needed in the whole app.
  49. */
  50. @interface OSSClient : NSObject
  51. /**
  52. OSS endpoint. It varies in different regions. Please check out OSS official website for the exact endpoints for your data.
  53. */
  54. @property (nonatomic, strong) NSString * endpoint;
  55. /**
  56. The networking instance for sending and receiving data
  57. */
  58. @property (nonatomic, strong) OSSNetworking * networking;
  59. /**
  60. The credential provider instance
  61. */
  62. @property (nonatomic, strong) id<OSSCredentialProvider> credentialProvider;
  63. /**
  64. Client configuration instance
  65. */
  66. @property (nonatomic, strong) OSSClientConfiguration * clientConfiguration;
  67. /**
  68. oss operation task queue
  69. */
  70. @property (nonatomic, strong, readonly) OSSExecutor * ossOperationExecutor;
  71. /**
  72. Initializes an OSSClient instance with the default client configuration.
  73. @endpoint it specifies domain of the bucket's region. Starting 2017, the domain must be prefixed with "https://" to follow Apple's ATS policy.
  74. For example: "https://oss-cn-hangzhou.aliyuncs.com"
  75. @credentialProvider The credential provider
  76. */
  77. - (instancetype)initWithEndpoint:(NSString *)endpoint
  78. credentialProvider:(id<OSSCredentialProvider>) credentialProvider;
  79. /**
  80. Initializes an OSSClient with the custom client configuration.
  81. @endpoint it specifies domain of the bucket's region. Starting 2017, the domain must be prefixed with "https://" to follow Apple's ATS policy.
  82. For example: "https://oss-cn-hangzhou.aliyuncs.com"
  83. @credentialProvider The credential provider
  84. @conf The custom client configuration such as retry time, timeout values, etc.
  85. */
  86. - (instancetype)initWithEndpoint:(NSString *)endpoint
  87. credentialProvider:(id<OSSCredentialProvider>)credentialProvider
  88. clientConfiguration:(OSSClientConfiguration *)conf;
  89. #pragma mark restful-api
  90. /**
  91. The corresponding RESTFul API: GetService
  92. Gets all the buckets of the current user
  93. Notes:
  94. 1. STS is not supported yet in this call.
  95. 2. When all buckets are returned, the xml in response body does not have nodes of Prefix, Marker, MaxKeys, IsTruncated and NextMarker.
  96. If there're remaining buckets to return, the xml will have these nodes. The nextMarker is the value of marker in the next call.
  97. */
  98. - (OSSTask *)getService:(OSSGetServiceRequest *)request;
  99. @end
  100. @interface OSSClient (Bucket)
  101. /**
  102. The corresponding RESTFul API: PutBucket
  103. Creates a bucket--it does not support anonymous access. By default, the datacenter used is oss-cn-hangzhou.
  104. Callers could explicitly specify the datacenter for the bucket to optimize the performance and cost or meet the regulation requirement.
  105. Notes:
  106. 1. STS is not supported yet.
  107. */
  108. - (OSSTask *)createBucket:(OSSCreateBucketRequest *)request;
  109. /**
  110. The corresponding RESTFul API: DeleteBucket
  111. Deletes a bucket.
  112. */
  113. - (OSSTask *)deleteBucket:(OSSDeleteBucketRequest *)request;
  114. /**
  115. The corresponding RESTFul API: GetBucket
  116. Lists all objects in a bucket. It could be specified with filters such as prefix, marker, delimeter and max-keys.
  117. */
  118. - (OSSTask *)getBucket:(OSSGetBucketRequest *)request;
  119. /**
  120. The corresponding RESTFul API: GetBucketInfo
  121. Gets the {@link Bucket}'s basic information as well as its ACL.
  122. */
  123. - (OSSTask *)getBucketInfo:(OSSGetBucketInfoRequest *)request;
  124. /**
  125. The corresponding RESTFul API: GetBucketACL
  126. Gets the bucket ACL.
  127. */
  128. - (OSSTask *)getBucketACL:(OSSGetBucketACLRequest *)request;
  129. @end
  130. @interface OSSClient (Object)
  131. /**
  132. The corresponding RESTFul API: HeadObject
  133. Gets the object's metadata information. The object's content is not returned.
  134. */
  135. - (OSSTask *)headObject:(OSSHeadObjectRequest *)request;
  136. /**
  137. The corresponding RESTFul API: GetObject
  138. Gets the whole object (includes content). It requires caller have read permission on the object.
  139. */
  140. - (OSSTask *)getObject:(OSSGetObjectRequest *)request;
  141. /**
  142. The corresponding RESTFul API: GetObjectACL
  143. get the acl of an object.
  144. */
  145. - (OSSTask *)getObjectACL:(OSSGetObjectACLRequest *)request;
  146. /**
  147. The corresponding RESTFul API: PutObject
  148. Uploads a file.
  149. */
  150. - (OSSTask *)putObject:(OSSPutObjectRequest *)request;
  151. /**
  152. Sets the object's ACL. Right now an object has three access permissions: private, public-ready, public-read-write.
  153. The operation specifies the x-oss-object-acl header in the put request. The caller must be the owner of the object.
  154. If succeeds, it returns HTTP status 200; otherwise it returns related error code and error messages.
  155. */
  156. - (OSSTask *)putObjectACL:(OSSPutObjectACLRequest *)request;
  157. /**
  158. The corresponding RESTFul API: AppendObject
  159. Appends data to an existing or non-existing object. The object created by this operation is appendable.
  160. As a comparison, the object created by Put Object is normal (non-appendable).
  161. */
  162. - (OSSTask *)appendObject:(OSSAppendObjectRequest *)request;
  163. /**
  164. * @brief Appends data to an existing or non-existing object on the OSS server.
  165. * The object created by this operation is appendable.
  166. * @request request
  167. * @crc64ecma crc64ecma
  168. * if object has been stored on OSS server, you need to invoke headObject
  169. * api get object's crc64ecma,then use this api to append data to the
  170. * object.
  171. */
  172. - (OSSTask *)appendObject:(OSSAppendObjectRequest *)request withCrc64ecma:(nullable NSString *)crc64ecma;
  173. /**
  174. The corresponding RESTFul API: copyObject
  175. Copies an existing object to another one.The operation sends a PUT request with x-oss-copy-source header to specify the source object.
  176. OSS server side will detect and copy the object. If it succeeds, the new object's metadata information will be returned.
  177. The operation applies for files less than 1GB. For big files, use UploadPartCopy RESTFul API.
  178. */
  179. - (OSSTask *)copyObject:(OSSCopyObjectRequest *)request;
  180. /**
  181. * Batch deletes the specified files under a specific bucket. If the files
  182. * are non-exist, the operation will still return successful.
  183. *
  184. * @param request
  185. * A OSSDeleteMultipleObjectsRequest instance which specifies the
  186. * bucket and file keys to delete.
  187. * @return A OSSTask with result of OSSDeleteMultipleObjectsResult instance which specifies each
  188. * file's result in normal mode or only failed deletions in quite
  189. * mode. By default it's quite mode.
  190. */
  191. - (OSSTask *)deleteMultipleObjects:(OSSDeleteMultipleObjectsRequest *)request;
  192. /**
  193. The corresponding RESTFul API: DeleteObject
  194. Deletes an object
  195. */
  196. - (OSSTask *)deleteObject:(OSSDeleteObjectRequest *)request;
  197. /**
  198. * Creates a symbol link to a target file under the bucket---this is not
  199. * supported for archive class bucket.
  200. *
  201. * @param request
  202. * A OSSPutSymlinkRequest instance that specifies the
  203. * bucket name, symlink name.
  204. * @return An instance of OSSTask. On successful execution, `task.result` will
  205. * contain an instance of `OSSPutSymlinkResult`,otherwise will contain
  206. * an instance of NSError.
  207. *
  208. * for more information,please refer to https://help.aliyun.com/document_detail/45126.html
  209. */
  210. - (OSSTask *)putSymlink:(OSSPutSymlinkRequest *)request;
  211. /**
  212. * Gets the symlink information for the given symlink name.
  213. *
  214. * @param request
  215. * A OSSGetSymlinkRequest instance which specifies the bucket
  216. * name and symlink name.
  217. * @return An instance of OSSTask. On successful execution, `task.result` will
  218. * contain an instance of `OSSGetSymlinkResult`,otherwise will contain
  219. * an instance of NSError.
  220. *
  221. * for more information,please refer to https://help.aliyun.com/document_detail/45146.html
  222. */
  223. - (OSSTask *)getSymlink:(OSSGetSymlinkRequest *)request;
  224. /**
  225. * Restores the object of archive storage. The function is not applicable to
  226. * Normal or IA storage. The restoreObject() needs to be called prior to
  227. * calling getObject() on an archive object.
  228. *
  229. * @param request
  230. * A container for the necessary parameters to execute the RestoreObject
  231. * service method.
  232. *
  233. * @return An instance of OSSTask. On successful execution, `task.result` will
  234. * contain an instance of `OSSRestoreObjectResult`,otherwise will contain
  235. * an instance of NSError.
  236. *
  237. * for more information,please refer to https://help.aliyun.com/document_detail/52930.html
  238. */
  239. - (OSSTask *)restoreObject:(OSSRestoreObjectRequest *)request;
  240. /**
  241. * You can call this operation to query the tags of an object.
  242. *
  243. * @param request
  244. * A OSSGetObjectTaggingRequest instance which specifies the bucket
  245. * name and object key.
  246. *
  247. * @return An instance of OSSTask. On successful execution, `task.result` will
  248. * contain an instance of `OSSGetObjectTaggingResult`,otherwise will contain
  249. * an instance of NSError.
  250. *
  251. * for more information,please refer to https://help.aliyun.com/document_detail/114878.html
  252. */
  253. - (OSSTask *)getObjectTagging:(OSSGetObjectTaggingRequest *)request;
  254. /**
  255. * You can call this operation to add tags to an object or update the tags added to
  256. * the bucket. The object tagging feature uses a key-value pair to tag an object.
  257. *
  258. * @param request
  259. * A OSSPutObjectTaggingRequest instance which specifies the bucket
  260. * name、object key and tags.
  261. *
  262. * @return An instance of OSSTask. On successful execution, `task.result` will
  263. * contain an instance of `OSSPutObjectTaggingResult`,otherwise will contain
  264. * an instance of NSError.
  265. *
  266. * for more information,please refer to https://help.aliyun.com/document_detail/114855.html
  267. */
  268. - (OSSTask *)putObjectTagging:(OSSPutObjectTaggingRequest *)request;
  269. /**
  270. * You can call this operation to delete the tags of a specified object.
  271. *
  272. * @param request
  273. * A OSSDeleteObjectTaggingRequest instance which specifies the bucket
  274. * name and object key.
  275. *
  276. * @return An instance of OSSTask. On successful execution, `task.result` will
  277. * contain an instance of `OSSDeleteObjectTaggingResult`,otherwise will contain
  278. * an instance of NSError.
  279. *
  280. * for more information,please refer to https://help.aliyun.com/document_detail/114879.html
  281. */
  282. - (OSSTask *)deleteObjectTagging:(OSSDeleteObjectTaggingRequest *)request;
  283. @end
  284. @interface OSSClient (MultipartUpload)
  285. /**
  286. The corresponding RESTFul API: InitiateMultipartUpload
  287. Initiates a multipart upload to get a upload Id. It's needed before starting uploading parts data.
  288. The upload Id is used for subsequential operations such as aborting the upload, querying the uploaded parts, etc.
  289. */
  290. - (OSSTask *)multipartUploadInit:(OSSInitMultipartUploadRequest *)request;
  291. /**
  292. The corresponding RESTFul API: UploadPart
  293. After the multipart upload is initiated, this API could be called to upload the data to the target file with the upload Id.
  294. Every uploaded part has a unique id called part number, which ranges from 1 to 10,000.
  295. For a given upload Id, the part number identifies the specific range of the data in the whole file.
  296. If the same part number is used for another upload, the existing data will be overwritten by the new upload.
  297. Except the last part, all other part's minimal size is 100KB.
  298. But no minimal size requirement on the last part.
  299. */
  300. - (OSSTask *)uploadPart:(OSSUploadPartRequest *)request;
  301. /**
  302. The corresponding RESTFul API: CompleteMultipartUpload
  303. This API is to complete the multipart upload after all parts data have been uploaded.
  304. It must be provided with a valid part list (each part has the part number and ETag).
  305. OSS will validate every part and then complete the multipart upload.
  306. If any part is invalid (e.g. the part is updated by another part upload), this API will fail.
  307. */
  308. - (OSSTask *)completeMultipartUpload:(OSSCompleteMultipartUploadRequest *)request;
  309. /**
  310. The corresponding RESTFul API: ListParts
  311. Lists all uploaded parts of the specified upload id.
  312. */
  313. - (OSSTask *)listParts:(OSSListPartsRequest *)request;
  314. /**
  315. The corresponding RESTFul API: ListMultipartUploads
  316. Lists all multipart uploads with the specified bucket.
  317. */
  318. - (OSSTask *)listMultipartUploads:(OSSListMultipartUploadsRequest *)request;
  319. /**
  320. The corresponding RESTFul API: AbortMultipartUpload
  321. Aborts the multipart upload by the specified upload Id.
  322. Once the multipart upload is aborted by this API, all parts data will be deleted and the upload Id is invalid anymore.
  323. */
  324. - (OSSTask *)abortMultipartUpload:(OSSAbortMultipartUploadRequest *)request;
  325. - (OSSTask *)abortResumableMultipartUpload:(OSSResumableUploadRequest *)request;
  326. /**
  327. Multipart upload API
  328. */
  329. - (OSSTask *)multipartUpload:(OSSMultipartUploadRequest *)request;
  330. /**
  331. TODOTODO
  332. Resumable upload API
  333. This API wraps the multipart upload and also enables resuming upload by reading/writing the checkpoint data.
  334. For a new file, multipartUploadInit() needs to be called first to get the upload Id. Then use this upload id to call this API to upload the data.
  335. If the upload fails, checks the error messages:
  336. If it's a recoverable error, then call this API again with the same upload Id to retry. The uploaded data will not be uploaded again.
  337. Otherwise then you may need to recreates a new upload Id and call this method again.
  338. Check out demo for the detail.
  339. */
  340. - (OSSTask *)resumableUpload:(OSSResumableUploadRequest *)request;
  341. /**
  342. * multipart upload sequentially in order,support resume upload
  343. */
  344. - (OSSTask *)sequentialMultipartUpload:(OSSResumableUploadRequest *)request;
  345. @end
  346. @interface OSSClient (PresignURL)
  347. /**
  348. Generates a signed URL for the object and anyone has this URL will get the GET permission on the object.
  349. @bucketName object's bucket name
  350. @objectKey Object name
  351. @interval Expiration time in seconds. The URL could be specified with the expiration time to limit the access window on the object.
  352. */
  353. - (OSSTask *)presignConstrainURLWithBucketName:(NSString *)bucketName
  354. withObjectKey:(NSString *)objectKey
  355. withExpirationInterval:(NSTimeInterval)interval;
  356. /**
  357. Generates a signed URL for the object and anyone has this URL will get the specified permission on the object.
  358. @bucketName object's bucket name
  359. @objectKey Object name
  360. @interval Expiration time in seconds. The URL could be specified with the expiration time to limit the access window on the object.
  361. @parameter it could specify allowed HTTP methods
  362. */
  363. - (OSSTask *)presignConstrainURLWithBucketName:(NSString *)bucketName
  364. withObjectKey:(NSString *)objectKey
  365. withExpirationInterval:(NSTimeInterval)interval
  366. withParameters:(NSDictionary *)parameters;
  367. /**
  368. Generates a signed URL for the object and anyone has this URL will get the specified permission on the object. currently only support get and head method.
  369. @bucketName object's bucket name
  370. @objectKey Object name
  371. @httpMethod http method.currently only support get and head.
  372. @interval Expiration time in seconds. The URL could be specified with the expiration time to limit the access window on the object.
  373. @parameter it could specify allowed HTTP methods
  374. */
  375. - (OSSTask *)presignConstrainURLWithBucketName:(NSString *)bucketName
  376. withObjectKey:(NSString *)objectKey
  377. httpMethod:(NSString *)method
  378. withExpirationInterval:(NSTimeInterval)interval
  379. withParameters:(NSDictionary *)parameters;
  380. /// Generates a signed URL for the object and anyone has this URL will get the specified permission on the object.
  381. /// @param bucketName object's bucket name
  382. /// @param objectKey Object name
  383. /// @param method http method.currently only support get and head.
  384. /// @param interval Expiration time in seconds. The URL could be specified with the expiration time to limit the access window on the object.
  385. /// @param parameters it could specify allowed HTTP methods
  386. /// @param contentType Content-Type to url sign
  387. /// @param contentMd5 Content-MD5 to url sign
  388. - (OSSTask *)presignConstrainURLWithBucketName:(NSString *)bucketName
  389. withObjectKey:(NSString *)objectKey
  390. httpMethod:(NSString *)method
  391. withExpirationInterval:(NSTimeInterval)interval
  392. withParameters:(NSDictionary *)parameters
  393. contentType:(nullable NSString *)contentType
  394. contentMd5:(nullable NSString *)contentMd5;
  395. /// Generates a signed URL for the object and anyone has this URL will get the specified permission on the object.
  396. /// @param bucketName object's bucket name
  397. /// @param objectKey Object name
  398. /// @param method http method.currently only support get and head.
  399. /// @param interval Expiration time in seconds. The URL could be specified with the expiration time to limit the access window on the object.
  400. /// @param parameters it could specify allowed HTTP methods
  401. /// @param headers Content Type, Content-MD5, and all HTTP headers prefixed with 'x-oss-*'
  402. - (OSSTask *)presignConstrainURLWithBucketName:(NSString *)bucketName
  403. withObjectKey:(NSString *)objectKey
  404. httpMethod:(NSString *)method
  405. withExpirationInterval:(NSTimeInterval)interval
  406. withParameters:(NSDictionary *)parameters
  407. withHeaders:(nullable NSDictionary *)headers;
  408. /**
  409. If the object's ACL is public read or public read-write, use this API to generate a signed url for sharing.
  410. @bucketName Object's bucket name
  411. @objectKey Object name
  412. */
  413. - (OSSTask *)presignPublicURLWithBucketName:(NSString *)bucketName
  414. withObjectKey:(NSString *)objectKey;
  415. /** TODOTODO
  416. If the object's ACL is public read or public read-write, use this API to generate a signed url for sharing.
  417. @bucketName Object's bucket name
  418. @objectKey Object name
  419. @parameter the request parameters.
  420. */
  421. - (OSSTask *)presignPublicURLWithBucketName:(NSString *)bucketName
  422. withObjectKey:(NSString *)objectKey
  423. withParameters:(NSDictionary *)parameters;
  424. @end
  425. @interface OSSClient (ImageService)
  426. /*
  427. * image persist action
  428. * https://help.aliyun.com/document_detail/55811.html
  429. */
  430. - (OSSTask *)imageActionPersist:(OSSImagePersistRequest *)request;
  431. @end
  432. @interface OSSClient (Utilities)
  433. /**
  434. Checks if the object exists
  435. @bucketName Object's bucket name
  436. @objectKey Object name
  437. return YES Object exists
  438. return NO && *error = nil Object does not exist
  439. return NO && *error != nil Error occured.
  440. */
  441. - (BOOL)doesObjectExistInBucket:(NSString *)bucketName
  442. objectKey:(NSString *)objectKey
  443. error:(const NSError **)error;
  444. @end
  445. @interface OSSClient (Callback)
  446. - (OSSTask *)triggerCallBack:(OSSCallBackRequest *)request;
  447. @end
  448. NS_ASSUME_NONNULL_END