OSS 画像処理 (IMG) を使用すると、標準 HTTP GET リクエストを使用して、OSS バケットに保存されているイメージをその場でサイズ変更、トリミング、回転、およびその他の変換を行うことができます。IMG パラメーターは、イメージオブジェクト URL のクエリ文字列で渡されます。
イメージオブジェクトのアクセス制御リスト (ACL) が非公開の場合、許可されるユーザーのみがアクセスできます。
前提条件
開始する前に、以下を確認してください。
少なくとも 1 つのイメージオブジェクトがアップロードされている OSS バケット
プロジェクトに OSS iOS SDK がインストールおよび設定済み
イメージダウンロード時の画像処理適用
匿名アクセス
presignPublicURLWithBucketName:withObjectKey:withParameters: を使用して、IMG パラメーターを含むパブリック URL を生成します。このアプローチは、公開読み取り ACL を持つオブジェクトにのみ機能します。
OSSTask *task = [client presignPublicURLWithBucketName:@"examplebucket"
withObjectKey:@"exampledir/example.jpg"
withParameters:@{@"x-oss-process": @"image/resize,w_50"}];
[task continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
NSLog(@"download image success!"); // イメージのダウンロードに成功しました!
OSSGetObjectResult *getResult = task.result;
NSLog(@"download image data: %@", getResult.downloadedData); // イメージデータ:
} else {
NSLog(@"download object failed, error: %@", task.error); // オブジェクトのダウンロードに失敗しました。エラー:
}
return nil;
}];x-oss-process パラメーター値は、image/<action>,<key>_<value> のフォーマットに従います。例えば、image/resize,w_50 はイメージの幅を 50 ピクセルにサイズ変更します。
認証済みアクセス
非公開 ACL を持つオブジェクトの場合、ダウンロード時に IMG パラメーターを適用するには、OSSGetObjectRequest で request.xOssProcess を設定します。
OSSGetObjectRequest *request = [OSSGetObjectRequest new];
// Bucket that contains the image // イメージを含むバケット
request.bucketName = @"examplebucket";
// Full path to the image. If the image is in the bucket root, omit the directory prefix. // イメージへのフルパス。イメージがバケットルートにある場合は、ディレクトリプレフィックスを省略します。
request.objectKey = @"exampledir/example.jpg";
// image/resize,m_lfit,w_100,h_100
// m_lfit → fit the image within the bounding box, preserving aspect ratio // イメージをバウンディングボックス内に収め、縦横比を維持します
// w_100 → maximum width: 100 px // 最大幅: 100 ピクセル
// h_100 → maximum height: 100 px // 最大高さ: 100 ピクセル
request.xOssProcess = @"image/resize,m_lfit,w_100,h_100";
OSSTask *getTask = [client getObject:request];
[getTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
NSLog(@"download image success!"); // イメージのダウンロードに成功しました!
OSSGetObjectResult *getResult = task.result;
NSLog(@"download image data: %@", getResult.downloadedData); // イメージデータ:
} else {
NSLog(@"download object failed, error: %@", task.error); // オブジェクトのダウンロードに失敗しました。エラー:
}
return nil;
}];
// Block the calling thread until the download completes (optional): // ダウンロードが完了するまで呼び出しスレッドをブロックします (オプション):
// [getTask waitUntilFinished];
// Cancel an in-progress download (optional): // 進行中のダウンロードをキャンセルします (オプション):
// [request cancel];処理済みイメージのバケットへの保存
OSSImagePersistRequest を使用して、IMG 操作を適用し、イメージをローカルにダウンロードせずに、結果を直接送信先バケットに書き込みます。
送信先バケット (toBucket) は、ソースバケット (fromBucket) と同じリージョンにある必要があります。
OSSImagePersistRequest *request = [OSSImagePersistRequest new];
// Source image // ソースイメージ
request.fromBucket = @"srcbucket";
request.fromObject = @"exampledir/src.jpg"; // Full path, or just "src.jpg" if in the bucket root // フルパス。バケットルートにある場合は "src.jpg" のみ
// Destination for the processed image (must be in the same region as fromBucket) // 処理済みイメージの送信先 (fromBucket と同じリージョンにある必要があります)
request.toBucket = @"destbucket";
request.toObject = @"exampledir/dest.jpg";
// image/resize,w_100 → resize to 100 px wide, preserving aspect ratio // 幅を 100 ピクセルにサイズ変更し、縦横比を維持します
request.action = @"image/resize,w_100";
// request.action = @"resize,w_100";
OSSTask *task = [client imageActionPersist:request];
[task continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
if (!task.error) {
NSLog(@"image saved successfully"); // イメージが正常に保存されました
} else {
NSLog(@"failed to save image, error: %@", task.error); // イメージの保存に失敗しました。エラー:
}
return nil;
}];IMG パラメーターを含む署名付き URL の生成
非公開イメージオブジェクトの場合、IMG パラメーターは URL 署名に含める必要があります。既に署名済みの URL に追加することはできません。presignConstrainURLWithBucketName:withObjectKey:withExpirationInterval:withParameters: を使用して、処理パラメーターを埋め込んだ署名付き URL を生成します。
NSString *bucketName = @"examplebucket";
NSString *objectKey = @"exampledir/example.jpg";
// Generate a signed URL valid for 1,800 seconds (30 minutes) // 1,800 秒 (30 分) 間有効な署名付き URL を生成します
// that resizes the image to 50 px wide (image/resize,w_50) // イメージの幅を 50 ピクセルにサイズ変更します (image/resize,w_50)
OSSTask *task = [_client presignConstrainURLWithBucketName:bucketName
withObjectKey:objectKey
withExpirationInterval:30 * 60
withParameters:@{@"x-oss-process": @"image/resize,w_50"}];
NSLog(@"url: %@", task.result);次のステップ
サポートされている IMG 操作とパラメーター構文の完全なリストについては、「IMG パラメーター」をご参照ください。