美文网首页
腾讯cos上传图片

腾讯cos上传图片

作者: 莫夏_b560 | 来源:发表于2018-10-22 16:07 被阅读0次

    1、上传路径"/...../....../....../a.jpg" 上传生成对应的文件目录

    2、获取图片可根据需求获取url

    public String getImgUrl(String key,boolean needSign){

    COSCredentials cred;

    if (needSign) {

    cred =new BasicCOSCredentials(secretId,secretKey);

    }

    else {

    cred =new AnonymousCOSCredentials();

    }

    ClientConfig clientConfig =new ClientConfig(new Region(region));

    clientConfig.setEndPointSuffix(endPoint);

    COSClient cosClient =new COSClient(cred, clientConfig);

    GeneratePresignedUrlRequest req =

    new GeneratePresignedUrlRequest(bucket, key, HttpMethodName.GET);

    //时效

        Date expirationDate =new Date(System.currentTimeMillis() +30L *60L *1000L);

    req.setExpiration(expirationDate);

    URL downloadUrl = cosClient.generatePresignedUrl(req);

    String url = downloadUrl.toString();

    url = url.replace(String.valueOf(new StringBuilder(bucket).append(".")),"");

    return url;

    }

    相关文章

      网友评论

          本文标题:腾讯cos上传图片

          本文链接:https://www.haomeiwen.com/subject/vsoghftx.html