You need to enable JavaScript to run this app.
导航
临时访问授权(iOS SDK)
最近更新时间:2024.02.04 18:30:59首次发布时间:2022.11.21 19:11:16

TosClient 在向服务端发起请求时,默认会在请求 Header 里包含签名。SDK 也支持构造带签名的 URL,可直接用该 URL 发起 HTTP 请求,也可以将该 URL 共享给第三方实现访问授权。

示例代码

如下代码展示如何生成带签名的 URL。

TOSCredential *credential = [[TOSCredential alloc] initWithAccessKey:@"accesskey" secretKey:@"secretkey"];
TOSEndpoint *tosEndpoint = [[TOSEndpoint alloc] initWithURLString:@"endpoint" withRegion:@"region"];
TOSClientConfiguration *config = [[TOSClientConfiguration alloc] initWithEndpoint:tosEndpoint credential:credential];
TOSClient *client = [[TOSClient alloc] initWithConfiguration:config];

TOSPreSignedURLInput *input = [TOSPreSignedURLInput new];
input.tosBucket = @"bucket-name";
input.tosKey = @"object-name";
input.tosHttpMethod = TOSHTTPMethodTypeGet;
TOSTask *task = [client preSignedURL:input];
[task continueWithBlock:^id _Nullable(TOSTask * _Nonnull task) {
    if (!task.error) {
        NSLog(@"Presigned success.");
        TOSPreSignedURLOutput *output = task.result;
        NSString *url = output.tosSignedUrl;
    } else {
        NSLog(@"Presigned failed, error: %@.", task.error);
    }
    return nil;
}];