本文檔主要以Java為例,簡述如何創建數據源并上傳文件;SDK獲取請參考鏈接

1. 創建默認配置文件

以下代碼用于創建默認配置文件。注意:accessKeyId和accessKeySecret前后都沒有空格,regionId(區域編碼)目前僅支持“cn-hangzhou”(下同); accessKeyId獲取請參考鏈接

        // 阿里云賬號AccessKey擁有所有API的訪問權限,風險很高。強烈建議您創建并使用RAM用戶進行API訪問或日常運維,請登錄RAM控制臺創建RAM用戶
        // 強烈建議不要把 AccessKey 和 AccessKeySecret 保存到代碼里,會存在密鑰泄漏風險 
        // 此處以把AccessKey 和 AccessKeySecret 保存在環境變量為例說明。您也可以根據業務需要,保存到配置文件里
        String accessKeyId = System.getenv("VCS_AK_ENV");
        String accessKeySecret = System.getenv("VCS_SK_ENV");
        // 創建默認配置文件
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        // 根據配置文件創建調用客戶端
        IAcsClient client = new DefaultAcsClient(profile);

2. 用戶創建租戶/空間

以下代碼用于添加租戶請求,您可以指定一些參數,詳情請見“視覺計算服務 > API參考 > 空間管理”;參考鏈接
        CreateCorpRequest cropRequest = new CreateCorpRequest();
        // 設置所在區域編碼 
        cropRequest.setRegionId(regionId);
        // 設置對應的租戶名稱
        cropRequest.setCorpName(cropName);
        // 設置對應的開發的應用名稱
        cropRequest.setAppName(appName);
 
        CreateCorpResponse cropResponse= null;
        try {
            // 客戶調用添加租戶接口得到調用返回結果
            cropResponse = client.getAcsResponse(cropRequest);
            // 判斷接口調用返回是否為空
            if (cropResponse == null) {
                return;
            }
            System.out.println(new Gson().toJson(cropResponse));
        } 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());
        }
3. 用戶創建數據源

以下代碼用于創建數據源,創建添加數據源請求,您可以指定一些參數,詳情請見“視覺計算服務 > API參考 > 數據源管理”,參考鏈接

注意:目前datasourceType(數據類型)暫時只支持目前支持PIC(圖片數據源)與AUDIO(音頻數據源),創建數據源完成之后請聯系您對接的阿里云同學配置相關計算任務后再上傳數據;
        AddDataSourceRequest dataSourceRequest = new AddDataSourceRequest();
        // 設置所在區域編碼
        dataSourceRequest.setRegionId(regionId);
        // 設置項目空間標識
        dataSourceRequest.setCorpId(cropResponse.getCorpId());
        // 設置數據源名稱
        dataSourceRequest.setDataSourceName(dataSourceName);
        // 設置數據源類型
        dataSourceRequest.setDataSourceType(dataSourceType);
        // 設置數據源描述
        dataSourceRequest.setDescription(description);
 
        AddDataSourceResponse dataSourceResponse = null;
        try {
            // 客戶調用添加數據源請求接口得到調用返回結果
            dataSourceResponse = client.getAcsResponse(dataSourceRequest);
            // 判斷接口返回是否為空
            if (dataSourceResponse == null || dataSourceResponse.getData() == null) {
                return;
            }
            System.out.println(new Gson().toJson(dataSourceResponse));
        } 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());
        }
 
 
4. 用戶指定數據源上傳文件

4.1 基于base64方式上傳文件

以下代碼用于通過base64加密方式創建文件上傳請求,您可以指定一些參數,詳情可見“視覺計算服務 > API參考 > 數據源管理,參考鏈接
        UploadFileRequest uploadFileRequest = new UploadFileRequest();
        // 設置所在低于編碼
        uploadFileRequest.setRegionId(regionId);
        // 設置文件類型
        uploadFileRequest.setFileType(fileType);
        // 設置項目空間標識
        uploadFileRequest.setCorpId(cropResponse.getCorpId());
        // 設置文件名稱
        uploadFileRequest.setFileName(fileName);
        // 設置數據源標識
        uploadFileRequest.setDataSourceId(dataSourceResponse.getData().getDataSourceId());
        // 設置MD5值
        uploadFileRequest.setMD5(MD5);
        // 設置圖片
        // 例如:/Users/yiming.zh/Desktop/vcg_VCG21gic16880301_RF.jpg
        uploadFileRequest.setFileContent(getPicBase64(picturePath));
    //上傳文件的OSS地址,格式bucketName/objectName;
       //uploadFileRequest.setFilePath(filePath);
 
        try {
            // 客戶調用文件上傳接口得到調用返回結果
            UploadFileResponse uploadFileResponse = client.getAcsResponse(uploadFileRequest);
            System.out.println(new Gson().toJson(uploadFileResponse));
        } 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());
        }
 
    }
 
 
    private static String getBase64(String filePath) {
        String imgStr = "";
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            int offset = 0;
            int numRead = 0;
            while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) {
                offset += numRead;
            }
 
            if (offset != buffer.length) {
                throw new IOException("Could not completely read file "
                        + file.getName());
            }
            fis.close();
            BASE64Encoder encoder = new BASE64Encoder();
            imgStr = encoder.encode(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return imgStr;
    }
 
    /**
     * 獲取文件內容的base64編碼結果
     *
     * @param filePath 文件本地地址
     * @return 文件內容base64編碼結果
     */
    private static String getPicBase64(String filePath) {
        return "data:image/jpeg;base64," + getBase64(filePath);
    }
                    
4.2 基于OSS文件的方式上傳文件
OSS文件授權完成后可進行文件請求,以下代碼用于通過OSS文件上傳方式創建文件上傳請求,您可以指定一些參數,詳情可見“視覺計算服務 > API參考 > 數據源管理,參考鏈接
    UploadFileRequest uploadFileRequest = new UploadFileRequest();
        // 設置所在低于編碼
        uploadFileRequest.setRegionId(regionId);
        // 設置文件類型
        uploadFileRequest.setFileType(fileType);
        // 設置項目空間標識
        uploadFileRequest.setCorpId(cropResponse.getCorpId());
        // 設置文件名稱
        uploadFileRequest.setFileName(fileName);
        // 設置數據源標識
        uploadFileRequest.setDataSourceId(dataSourceResponse.getData().getDataSourceId());
        // 設置MD5值
        uploadFileRequest.setMD5(MD5);
        // 設置圖片
        // 例如:/Users/yiming.zh/Desktop/vcg_VCG21gic16880301_RF.jpg
        uploadFileRequest.setFileContent(getPicBase64(picturePath));
    //上傳文件的OSS地址,格式bucketName/objectName;
       //uploadFileRequest.setFilePath(filePath);
 
        try {
            // 客戶調用文件上傳接口得到調用返回結果
            UploadFileResponse uploadFileResponse = client.getAcsResponse(uploadFileRequest);
            System.out.println(new Gson().toJson(uploadFileResponse));
        } 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());
        }
 
    }
完整代碼如下:
package com.aliyun.vcs.demo;
 
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.aliyuncs.vcs.model.v20200515.*;
import com.google.gson.Gson;
import sun.misc.BASE64Encoder;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
 
 
public class HelloVCS {
 
    // 用戶所在對應的地區編碼
    private static String regionId = "<yourRegionId>";
 
    // 對應的租戶名稱
    private static String cropName = "<yourCropName>";
 
    // 開發的應用名稱,如社區衛士
    private static String appName = "<yourAppName>";
 
    // 數據源名稱
    private static String dataSourceName = "<yourDataSourceName>";
 
    // 數據源類型
    // 音頻數據源:AUDIO;視頻數據源:VIDEO;實體設備:DEVICE;圖片數據源:PIC
    private static String dataSourceType = "<yourDataSourceType>";
 
    // 數據源描述
    private static String description = "<yourDescription>";
 
    // 文件類型
    // 音頻數據源:AUDIO;視頻數據源:VIDEO;實體設備:DEVICE;圖片數據源:PIC
    private static String fileType = "<yourFileType>";
 
    // 上傳文件的名稱
    private static String fileName = "<yourFileName>";
 
    // 文件MD5值
    private static String MD5 = "<yourMD5>";
 
    // 上傳圖片路徑
    // 例如:/Users/yiming.zh/Desktop/vcg_VCG21gic16880301_RF.jpg
    private static String picturePath = "<yourPicturePath>";
 
    // 上傳文件的OSS地址,格式 bucketName/objectName
    //private static String filePath = "<yourFilePath>";
 
 
    public static void main(String[] args) {
        // 阿里云賬號AccessKey擁有所有API的訪問權限,風險很高。強烈建議您創建并使用RAM用戶進行API訪問或日常運維,請登錄RAM控制臺創建RAM用戶
        // 強烈建議不要把 AccessKey 和 AccessKeySecret 保存到代碼里,會存在密鑰泄漏風險 
        // 此處以把AccessKey 和 AccessKeySecret 保存在環境變量為例說明。您也可以根據業務需要,保存到配置文件里
        String accessKeyId = System.getenv("VCS_AK_ENV");
        String accessKeySecret = System.getenv("VCS_SK_ENV");
 
        // 創建默認配置文件
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        // 根據配置文件創建調用客戶端
        IAcsClient client = new DefaultAcsClient(profile);
 
        // 創建添加租戶請求。您可以指定一些參數,詳見“視覺計算服務 > API參考 > 空間管理”,
        // 鏈接地址是:http://bestwisewords.com/document_detail/165193.html?spm=a2c4g.11186623.6.558.6a8b793dqrEDdo
        CreateCorpRequest cropRequest = new CreateCorpRequest();
        // 設置所在區域編碼
        cropRequest.setRegionId(regionId);
        // 設置對應的租戶名稱
        cropRequest.setCorpName(cropName);
        // 設置對應的開發的應用名稱
        cropRequest.setAppName(appName);
 
        CreateCorpResponse cropResponse= null;
        try {
            // 客戶調用添加租戶接口得到調用返回結果
            cropResponse = client.getAcsResponse(cropRequest);
            // 判斷接口調用返回是否為空
            if (cropResponse == null) {
                return;
            }
            System.out.println(new Gson().toJson(cropResponse));
        } 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());
        }
 
        // 創建添加數據源請求。您可以指定一些參數,詳見“視覺計算服務 > API參考 > 數據源管理”,
        // 鏈接地址是:http://bestwisewords.com/document_detail/173291.html?spm=a2c4g.11186623.6.584.4cdd49205xKLBs
        AddDataSourceRequest dataSourceRequest = new AddDataSourceRequest();
        // 設置所在區域編碼
        dataSourceRequest.setRegionId(regionId);
        // 設置項目空間標識
        dataSourceRequest.setCorpId(cropResponse.getCorpId());
        // 設置數據源名稱
        dataSourceRequest.setDataSourceName(dataSourceName);
        // 設置數據源類型
        dataSourceRequest.setDataSourceType(dataSourceType);
        // 設置數據源描述
        dataSourceRequest.setDescription(description);
 
        AddDataSourceResponse dataSourceResponse = null;
        try {
            // 客戶調用添加數據源請求接口得到調用返回結果
            dataSourceResponse = client.getAcsResponse(dataSourceRequest);
            // 判斷接口返回是否為空
            if (dataSourceResponse == null || dataSourceResponse.getData() == null) {
                return;
            }
            System.out.println(new Gson().toJson(dataSourceResponse));
        } 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());
        }
 
        // 創建文件上傳請求。您可以指定一些參數,詳見“視覺計算服務 > API參考 > 數據源管理”,
        // 鏈接地址是:http://bestwisewords.com/document_detail/173292.html?spm=a2c4g.11186623.6.583.78db74c2GyGlNl
        UploadFileRequest uploadFileRequest = new UploadFileRequest();
        // 設置所在低于編碼
        uploadFileRequest.setRegionId(regionId);
        // 設置文件類型
        uploadFileRequest.setFileType(fileType);
        // 設置項目空間標識
        uploadFileRequest.setCorpId(cropResponse.getCorpId());
        // 設置文件名稱
        uploadFileRequest.setFileName(fileName);
        // 設置數據源標識
        uploadFileRequest.setDataSourceId(dataSourceResponse.getData().getDataSourceId());
        // 設置MD5值
        uploadFileRequest.setMD5(MD5);
        // 設置圖片
        // 例如:/Users/yiming.zh/Desktop/vcg_VCG21gic16880301_RF.jpg
        uploadFileRequest.setFileContent(getPicBase64(picturePath));
    //上傳文件的OSS地址,格式bucketName/objectName;
       //uploadFileRequest.setFilePath(filePath);
 
        try {
            // 客戶調用文件上傳接口得到調用返回結果
            UploadFileResponse uploadFileResponse = client.getAcsResponse(uploadFileRequest);
            System.out.println(new Gson().toJson(uploadFileResponse));
        } 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());
        }
 
    }
 
 
    private static String getBase64(String filePath) {
        String imgStr = "";
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            int offset = 0;
            int numRead = 0;
            while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) {
                offset += numRead;
            }
 
            if (offset != buffer.length) {
                throw new IOException("Could not completely read file "
                        + file.getName());
            }
            fis.close();
            BASE64Encoder encoder = new BASE64Encoder();
            imgStr = encoder.encode(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return imgStr;
    }
 
    /**
     * 獲取文件內容的base64編碼結果
     *
     * @param filePath 文件本地地址
     * @return 文件內容base64編碼結果
     */
    private static String getPicBase64(String filePath) {
        return "data:image/jpeg;base64," + getBase64(filePath);
    }
 
 
 
}