forked from tencentyun/cos-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectACL.m
140 lines (108 loc) · 5.04 KB
/
ObjectACL.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#import <XCTest/XCTest.h>
#import <QCloudCOSXML/QCloudCOSXML.h>
#import <QCloudCOSXML/QCloudUploadPartRequest.h>
#import <QCloudCOSXML/QCloudCompleteMultipartUploadRequest.h>
#import <QCloudCOSXML/QCloudAbortMultipfartUploadRequest.h>
#import <QCloudCOSXML/QCloudMultipartInfo.h>
#import <QCloudCOSXML/QCloudCompleteMultipartUploadInfo.h>
@interface ObjectACL : XCTestCase <QCloudSignatureProvider, QCloudCredentailFenceQueueDelegate>
@property (nonatomic) QCloudCredentailFenceQueue* credentialFenceQueue;
@end
@implementation ObjectACL
- (void)setUp {
// 注册默认的 COS 服务
QCloudServiceConfiguration* configuration = [QCloudServiceConfiguration new];
configuration.appID = @"1250000000";
configuration.signatureProvider = self;
QCloudCOSXMLEndPoint* endpoint = [[QCloudCOSXMLEndPoint alloc] init];
endpoint.regionName = @"ap-guangzhou";//服务地域名称,可用的地域请参考注释
configuration.endpoint = endpoint;
[QCloudCOSXMLService registerDefaultCOSXMLWithConfiguration:configuration];
[QCloudCOSTransferMangerService registerDefaultCOSTransferMangerWithConfiguration:configuration];
// 脚手架用于获取临时密钥
self.credentialFenceQueue = [QCloudCredentailFenceQueue new];
self.credentialFenceQueue.delegate = self;
}
- (void) fenceQueue:(QCloudCredentailFenceQueue * )queue requestCreatorWithContinue:(QCloudCredentailFenceQueueContinue)continueBlock
{
QCloudCredential* credential = [QCloudCredential new];
//在这里可以同步过程从服务器获取临时签名需要的 secretID,secretKey,expiretionDate 和 token 参数
credential.secretID = @"COS_SECRETID";
credential.secretKey = @"COS_SECRETKEY";
credential.token = @"COS_TOKEN";
/*强烈建议返回服务器时间作为签名的开始时间,用来避免由于用户手机本地时间偏差过大导致的签名不正确 */
credential.startDate = [[[NSDateFormatter alloc] init] dateFromString:@"startTime"]; // 单位是秒
credential.experationDate = [[[NSDateFormatter alloc] init] dateFromString:@"expiredTime"];
QCloudAuthentationV5Creator* creator = [[QCloudAuthentationV5Creator alloc]
initWithCredential:credential];
continueBlock(creator, nil);
}
- (void) signatureWithFields:(QCloudSignatureFields*)fileds
request:(QCloudBizHTTPRequest*)request
urlRequest:(NSMutableURLRequest*)urlRequst
compelete:(QCloudHTTPAuthentationContinueBlock)continueBlock
{
[self.credentialFenceQueue performAction:^(QCloudAuthentationCreator *creator,
NSError *error) {
if (error) {
continueBlock(nil, error);
} else {
QCloudSignature* signature = [creator signatureForData:urlRequst];
continueBlock(signature, nil);
}
}];
}
/**
* 设置对象访问权限控制列表
*/
- (void)putObjectAcl {
//.cssg-snippet-body-start:[objc-put-object-acl]
QCloudPutObjectACLRequest* request = [QCloudPutObjectACLRequest new];
// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "dir1/object1"
request.object = @"exampleobject";
// 存储桶名称,格式为 BucketName-APPID
request.bucket = @"examplebucket-1250000000";
NSString *grantString = [NSString stringWithFormat:@"id=\"%@\"",@"100000000001"];
// grantFullControl 等价于 grantRead + grantWrite
// 赋予被授权者读写权限。
request.grantFullControl = grantString;
// 赋予被授权者读权限。
request.grantRead = grantString;
// 赋予被授权者写权限。
request.grantWrite = grantString;
[request setFinishBlock:^(id outputObject, NSError *error) {
// 可以从 outputObject 中获取 response 中 etag 或者自定义头部等信息
NSDictionary* info = (NSDictionary *) outputObject;
}];
[[QCloudCOSXMLService defaultCOSXML] PutObjectACL:request];
//.cssg-snippet-body-end
}
/**
* 查询对象访问权限控制列表
*/
- (void)getObjectAcl {
//.cssg-snippet-body-start:[objc-get-object-acl]
QCloudGetObjectACLRequest *request = [QCloudGetObjectACLRequest new];
// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "dir1/object1"
request.object = @"exampleobject";
// 存储桶名称,格式为 BucketName-APPID
request.bucket = @"examplebucket-1250000000";
__block QCloudACLPolicy* policy;
[request setFinishBlock:^(QCloudACLPolicy * _Nonnull result,
NSError * _Nonnull error) {
policy = result;
// result.accessControlList; 被授权者与权限的信息
// result.owner; 持有者的信息
}];
[[QCloudCOSXMLService defaultCOSXML] GetObjectACL:request];
//.cssg-snippet-body-end
}
// .cssg-methods-pragma
- (void)testObjectACL {
// 设置对象 ACL
[self putObjectAcl];
// 获取对象 ACL
[self getObjectAcl];
// .cssg-methods-pragma
}
@end