java-sdk icon indicating copy to clipboard operation
java-sdk copied to clipboard

sdk 上传图片报错

Open freezhan opened this issue 9 years ago • 8 comments

sdk 版本 7.1.3

错误日志:

com.qiniu.common.QiniuException
	at com.qiniu.http.Client.send(Client.java:245)
	at com.qiniu.http.Client.multipartPost(Client.java:218)
	at com.qiniu.http.Client.multipartPost(Client.java:186)
	at com.qiniu.storage.FormUploader.upload(FormUploader.java:52)
	at com.qiniu.storage.UploadManager.put(UploadManager.java:126)
	at com.qiniu.storage.UploadManager.put(UploadManager.java:104)
	at io.cloudinsight.alert.service.impl.QiniuImageStoreService.upload(QiniuImageStoreService.java:40)

报错挺频繁的

freezhan avatar Mar 01 '17 08:03 freezhan

换最新版本 7.2.5 后依旧有这个错误

freezhan avatar Mar 01 '17 09:03 freezhan

麻烦使用

System.err.println(ex.response)

打印下具体错误信息

jemygraw avatar Mar 02 '17 08:03 jemygraw

{ResponseInfo:com.qiniu.http.Response@1bfbe7fc,status:401, reqId:lV4AAJqVlOxMg60U, xlog:body:1;UP:1/401;UP:2/401, xvia:, adress:up-z1.qiniu.com/111.206.234.140:80, duration:0.000000 s, error:expired token}

freezhan avatar Mar 20 '17 06:03 freezhan

相关代码如下:

@PostConstruct
public void init() {
    // for testing
    Auth auth = Auth.create(qiniuAccessKey, qiniuSecretKey);
    this.token = auth.uploadToken(qiniuBucket);

    Zone zone = Zone.autoZone();
    Configuration configuration = new Configuration(zone);
    uploadManager = new UploadManager(configuration);
}

@Override
public boolean upload(byte[] imageBytes, String imageKey) {
    try {
        Response r = uploadManager.put(imageBytes, imageKey + ".png", this.token);
        if (!r.isOK()) {
            log.error("acesskey: {}, secretKey: {}, bucket: {}.", qiniuAccessKey, qiniuSecretKey, qiniuBucket);
        }
        return true;
    } catch (QiniuException e) {
        log.error("qiniu upload response error: {}.", e.response);
        e.printStackTrace();
    }
    return false;
}

参考实例: https://github.com/qiniu/java-sdk/blob/master/examples/upload.java

freezhan avatar Mar 20 '17 06:03 freezhan

除了选择设置 ttl 非常大之外,可否增加 token 永不过期~

freezhan avatar Mar 20 '17 06:03 freezhan

使用 https://github.com/qiniu/java-sdk/blob/master/src/main/java/com/qiniu/util/Auth.java 生成的token好像是有问题的,我使用 https://developer.qiniu.com/kodo/manual/1208/upload-token 这个页面在线生成的tokan传入能够上传图片成功,使用Auth.java类生成的tokan就报错401

ahjsrhj avatar Apr 20 '17 06:04 ahjsrhj

key 是否有填错,我用过好几个版本,Auth.java 生成的 token 都没问题。

freezhan avatar Apr 26 '17 10:04 freezhan

sorry, 具体原因忘记了,应该不是sdk的原因,我后来自己简写了这个过程,能够工作,就没有细究,

    public static String getToken(String bucket, String fileType) {
        long time = System.currentTimeMillis();
        String scope = bucket;
        long deadLine = time / 1000 + 3600;     // 超时时间 1小时;S


        JsonObject object = new JsonObject();
        object.addProperty("scope", scope);
        object.addProperty("deadline", deadLine);
        String json = object.toString();

        String encodedPutPolicy = UrlSafeBase64.encodeToString(json);

        try {
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(SK);
            String encodedSign = UrlSafeBase64.encodeToString(mac.doFinal(StringUtils.toUtf8Bytes(encodedPutPolicy)));

            return AK + ':' + encodedSign + ':' + encodedPutPolicy;
        } catch (NoSuchAlgorithmException | InvalidKeyException e) {
            e.printStackTrace();
        }
        return null;
    }

ahjsrhj avatar Apr 27 '17 01:04 ahjsrhj