• 使用了腾讯的对象存储,使用cos_api时老是报错:
    please make sure bucket name must contain legal appid when appid is missing. example: music-12511223
    但是我确认了以后我的存储桶和appid都是正确的,如图:
    在这里插入图片描述
  • 折磨了好久,静下心来Debug了一波,进入COSClient类后,终于发现了问题:
    这是使用api时倒入的cos_api和版本
<dependency>
      <groupId>com.qcloud</groupId>
      <artifactId>cos_api</artifactId>
      <version>5.2.4</version>
</dependency>
这是旧版本的格式化桶代码(formatBucket)
private String formatBucket(String bucketName, String appid) throws CosClientException {
        BucketNameUtils.validateBucketName(bucketName);
        String parrtern;
        if (appid == null) {
            parrtern = ".*-(125|100|20)[0-9]{3,}$";
            if (Pattern.matches(parrtern, bucketName)) {
                return bucketName;
            } else {
                throw new CosClientException("please make sure bucket name must contain legal appid when appid is missing. example: music-1251122334");
            }
        } else {
            parrtern = "-" + appid;
            return bucketName.endsWith(parrtern) ? bucketName : bucketName + parrtern;
        }
    }

当前的版本太低,正则表达式“-”后面是(125 100 20 )开头的,我的appid是 130 开头的
更换了新的版本后,正则表达式变了

这是新版本的格式化桶代码(formatBucket)
private String formatBucket(String bucketName, String appid) throws CosClientException {
        BucketNameUtils.validateBucketName(bucketName);
        String parrtern;
        if (appid == null) {
            parrtern = ".*-[0-9]{3,}$";
            if (Pattern.matches(parrtern, bucketName)) {
                return bucketName;
            } else {
                throw new CosClientException("please make sure bucket name must contain legal appid when appid is missing. example: music-1251122334");
            }
        } else {
            parrtern = "-" + appid;
            return bucketName.endsWith(parrtern) ? bucketName : bucketName + parrtern;
        }
    }

新版本贴上

<dependency>
            <groupId>com.qcloud</groupId>
            <artifactId>cos_api</artifactId>
            <version>5.6.8</version>
        </dependency>

希望对大家有所帮助

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐