本文档主要以java为例,简述如何创建数据源并上传文件;SDK获取请参考链接

1. 创建默认配置文件

以下代码用于创建默认配置文件注意:accessKeyId和accessKeySecret前后都没有空格,regionId(区域编码)目前仅支持“cn-hangzhou”(下同); daccessKeyId获取请参考链接

        // 创建默认配置文件
        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 {
 
    // 注意:accessKeyId和accessKeySecret前后都没有空格
    // accessKeyId获取请参考链接:
    // https://help.aliyun.com/document_detail/53045.html?spm=a2c4g.11186623.2.28.74e451d2AkCL8T#concept-53045-zh
    private static String accessKeyId = "<yourAccessKeyId>";
    private static String accessKeySecret = "<yourAccessKeySecret>";
 
    // 用户所在对应的地区编码
    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) {
 
        // 创建默认配置文件
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        // 根据配置文件创建调用客户端
        IAcsClient client = new DefaultAcsClient(profile);
 
        // 创建添加租户请求。您可以指定一些参数,详见“视觉计算服务 > API参考 > 空间管理”,
        // 链接地址是:https://help.aliyun.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参考 > 数据源管理”,
        // 链接地址是:https://help.aliyun.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参考 > 数据源管理”,
        // 链接地址是:https://help.aliyun.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);
    }
 
 
 
}