当前位置: 代码网 > 移动>腾讯>腾讯游戏 > 腾讯云COS上传文件出现数据损坏问题

腾讯云COS上传文件出现数据损坏问题

2024年08月02日 腾讯游戏 我要评论
给metadata的ContentLength属性赋值所要传输文件字节流的字节数.桶中能正常接收到文件,但是文件出现数据损坏,控制台报错为。另外COS上传文件的工具类分享给大家.

问题:

解决办法 :

给metadata的contentlength属性赋值所要传输文件字节流的字节数.

objectmetadata metadata = new objectmetadata();
metadata.setcontentlength(bytearrayinputstream.available());

另外cos上传文件的工具类分享给大家.

@component
@configurationproperties(prefix = "tencent")
@data
public class tencentcosproperties {

    private string endpoint;
    private string accesskeyid;
    private string accesskeysecret;
    private string bucketname;
}
=======================================================
@configuration
@slf4j
public class cosconfiguration {

    @bean
    @conditionalonmissingbean
    public tencentcosutil tencentcosutil(tencentcosproperties tencentcosproperties){
        return new tencentcosutil(
                tencentcosproperties.getendpoint(),
                tencentcosproperties.getaccesskeyid(),
                tencentcosproperties.getaccesskeysecret(),
                tencentcosproperties.getbucketname());
    }
}
=======================================================
@data
@allargsconstructor
@slf4j
public class tencentcosutil {

    private string endpoint;
    private string accesskeyid;
    private string accesskeysecret;
    private string bucketname;

    /**
     * 文件上传
     *
     * @param bytes 文件byte数组
     * @param objectname   文件名称
     * @return
     */
    public string upload(byte[] bytes, string objectname) {

        // 创建ossclient实例。
        coscredentials cred = new basiccoscredentials(accesskeyid, accesskeysecret);
        region region = new region(com.tencentcloudapi.common.profile.region.guangzhou.tostring());
        clientconfig clientconfig = new clientconfig(region);
        clientconfig.sethttpprotocol(httpprotocol.https);
        cosclient cosclient = new cosclient(cred, clientconfig);

        string folder = "images/";
        string key = folder + objectname;
        objectmetadata metadata = new objectmetadata();
        metadata.setcontenttype("image/png");
        bytearrayinputstream bytearrayinputstream = new bytearrayinputstream(bytes);
        metadata.setcontentlength(bytearrayinputstream.available());
        putobjectrequest putobjectrequest = new putobjectrequest(bucketname, key, bytearrayinputstream, metadata);
        putobjectresult putobjectresult = cosclient.putobject(putobjectrequest);

        //文件访问路径规则 https://bucketname.endpoint/objectname
        stringbuilder stringbuilder = new stringbuilder("");
        stringbuilder
                .append(endpoint)
                .append("/")
                .append(key);

        log.info("文件上传到:{}", stringbuilder.tostring());
        cosclient.shutdown();
        return stringbuilder.tostring();
    }
}

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com