Search in sources :

Example 1 with UploadFileRequest

use of com.qcloud.cos.request.UploadFileRequest in project mall by wangxl-git.

the class QcloudCloudStorageService method upload.

@Override
public String upload(byte[] data, String path) {
    // 腾讯云必需要以"/"开头
    if (!path.startsWith("/")) {
        path = "/" + path;
    }
    // 上传到腾讯云
    UploadFileRequest request = new UploadFileRequest(config.getQcloudBucketName(), path, data);
    String response = client.uploadFile(request);
    JSONObject jsonObject = JSONObject.parseObject(response);
    if (jsonObject.getInteger("code") != 0) {
        throw new RRException("文件上传失败," + jsonObject.getString("message"));
    }
    return config.getQcloudDomain() + path;
}
Also used : UploadFileRequest(com.qcloud.cos.request.UploadFileRequest) JSONObject(com.alibaba.fastjson.JSONObject) RRException(io.renren.common.exception.RRException)

Example 2 with UploadFileRequest

use of com.qcloud.cos.request.UploadFileRequest in project xian by happyyangyuan.

the class CosFileWriter method forPath.

public boolean forPath(String path, String data) {
    try {
        checkPath(path);
        // 初始化cosClient,如果没有初始化的话
        getCosClient();
        UploadFileRequest request = new UploadFileRequest(BUCKET, getBase() + path, data.getBytes());
        request.setInsertOnly(InsertOnly.OVER_WRITE);
        return doUpload(request, 3);
    } catch (Throwable e) {
        LOG.error(e);
        return false;
    }
}
Also used : UploadFileRequest(com.qcloud.cos.request.UploadFileRequest)

Example 3 with UploadFileRequest

use of com.qcloud.cos.request.UploadFileRequest in project xian by happyyangyuan.

the class CosFileWriter method main.

public static void main(String[] args) {
    COSClient cosClient = new COSClient(10053621, "AKID5iJcsYewRYIJhqQsoaLQ7Ks1XIO6eYPs", "Gm0nqHOPzUG1MRRJnBLX4UwwQMoh8v4y");
    ClientConfig config = new ClientConfig();
    config.setRegion("sh");
    cosClient.setConfig(config);
    UploadFileRequest request = new UploadFileRequest("xian", "/xian_runtime_IDE_happyyangyuan/xian/yy/991.txt", "测试data".getBytes());
    request.setInsertOnly(InsertOnly.OVER_WRITE);
    String uploadFileRet = cosClient.uploadFile(request);
    LOG.info("上传文件返回:" + uploadFileRet);
    GetFileInputStreamRequest inputStreamRequest = new GetFileInputStreamRequest("xian", "/xian_runtime_IDE_happyyangyuan/xian/yy/991.txt");
    inputStreamRequest.setUseCDN(false);
    try (InputStream inputStream = cosClient.getFileInputStream(inputStreamRequest)) {
        System.out.println("读文件:" + StringIO.readFully(inputStream));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : COSClient(com.qcloud.cos.COSClient) UploadFileRequest(com.qcloud.cos.request.UploadFileRequest) InputStream(java.io.InputStream) ClientConfig(com.qcloud.cos.ClientConfig) GetFileInputStreamRequest(com.qcloud.cos.request.GetFileInputStreamRequest)

Aggregations

UploadFileRequest (com.qcloud.cos.request.UploadFileRequest)3 JSONObject (com.alibaba.fastjson.JSONObject)1 COSClient (com.qcloud.cos.COSClient)1 ClientConfig (com.qcloud.cos.ClientConfig)1 GetFileInputStreamRequest (com.qcloud.cos.request.GetFileInputStreamRequest)1 RRException (io.renren.common.exception.RRException)1 InputStream (java.io.InputStream)1