Untuk mengunggah sumber daya media menggunakan URL unggah dan kredensial, Anda perlu men-deploy layanan otorisasi pada server aplikasi Anda. Layanan ini memperoleh URL unggah dan kredensial dari ApsaraVideo VOD serta menyediakannya kepada klien Anda.
Informasi latar belakang
Sebelum memulai, pahami terlebih dahulu proses unggah secara keseluruhan yang menggunakan URL unggah dan kredensial.
Mendapatkan URL unggah dan kredensial
Server aplikasi Anda harus memanggil operasi OpenAPI yang berbeda untuk memperoleh URL dan kredensial unggah sesuai jenis media.
Untuk mendapatkan URL unggah dan kredensial file audio atau video, panggil operasi CreateUploadVideo.
Untuk merefresh kredensial unggah file audio atau video, panggil operasi RefreshUploadVideo.
Untuk mendapatkan URL unggah dan kredensial gambar, panggil operasi CreateUploadImage.
Untuk mendapatkan URL unggah dan kredensial aset media pendukung, panggil operasi CreateUploadAttachedMedia.
Langkah 1: Siapkan pengguna RAM
Buat Resource Access Management (RAM) user dan perhatikan hal-hal berikut:
Aktifkan Programmatic Access.
Simpan pasangan AccessKey yang Anda peroleh secara aman.
Langkah 2: Berikan izin kepada pengguna RAM
Berikan izin kepada RAM user untuk memanggil operasi OpenAPI yang diperlukan. Lampirkan salah satu kebijakan sistem berikut sesuai kebutuhan. Untuk informasi selengkapnya, lihat Manage permissions for a RAM user.
Nama Kebijakan | Deskripsi |
AliyunVodFullAccess | Izin untuk mengelola ApsaraVideo VOD. |
AliyunVodUploadAuth | Izin untuk mengunggah ke ApsaraVideo VOD. |
Langkah 3: Panggil operasi OpenAPI
Topik ini menyediakan contoh Java yang menunjukkan cara mendapatkan URL unggah dan kredensial untuk resource video. Sebelum memulai, konfigurasikan pasangan AccessKey di lingkungan sistem Anda. Anda dapat memodifikasi kode contoh sesuai kebutuhan untuk menyediakan operasi API bagi client Anda.
SDK V1.0
Tambahkan dependensi berikut dalam Maven:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.7.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version>2.16.32</version>
</dependency>import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.vod.model.v20170321.*;
/*
pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.0</version>
</dependency>
*/
public class CreateUploadVideo {
public static void main(String[] args) {
// Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID dan ALIBABA_CLOUD_ACCESS_KEY_SECRET telah disetel.
DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
IAcsClient client = new DefaultAcsClient(profile);
CreateUploadVideoRequest request = new CreateUploadVideoRequest();
request.setFileName("aliyun.mp4");
request.setTitle("aliyun");
try {
CreateUploadVideoResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}
SDK V2.0
Tambahkan dependensi berikut dalam Maven:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>vod20170321</artifactId>
<version>3.6.4</version>
</dependency>Untuk keamanan yang lebih baik, gunakan metode tanpa kredensial dalam kode proyek Anda. Untuk informasi selengkapnya tentang konfigurasi kredensial, lihat Manage access credentials.
// File ini dibuat otomatis, jangan mengeditnya. Terima kasih.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* description :
* <p>Inisialisasi Client akun dengan kredensial</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.vod20170321.Client createClient() throws Exception {
com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setCredential(credential);
// Untuk endpoint, lihat https://api.aliyun.com/product/vod
config.endpoint = "vod.cn-shanghai.aliyuncs.com";
return new com.aliyun.vod20170321.Client(config);
}
public static void main(String[] args_) throws Exception {
com.aliyun.vod20170321.Client client = Sample.createClient();
com.aliyun.vod20170321.models.CreateUploadVideoRequest createUploadVideoRequest = new com.aliyun.vod20170321.models.CreateUploadVideoRequest()
.setFileName("aliyun.mp4")
.setTitle("aliyun");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
com.aliyun.vod20170321.models.CreateUploadVideoResponse resp = client.createUploadVideoWithOptions(createUploadVideoRequest, runtime);
com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(resp));
} catch (TeaException error) {
// Ini hanya untuk tujuan demonstrasi. Tangani exception dengan hati-hati dan jangan pernah mengabaikannya langsung di proyek produksi.
// Pesan error
System.out.println(error.getMessage());
// URL diagnosa
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// Ini hanya untuk tujuan demonstrasi. Tangani exception dengan hati-hati dan jangan pernah mengabaikannya langsung di proyek produksi.
// Pesan error
System.out.println(error.getMessage());
// URL diagnosa
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}