七牛通过bucket:key获取上传凭证更新图片失败
程序员文章站
2022-04-10 18:10:51
...
我希望通过设置key为bucket:key的方式来获取可用于更新的文件token,但是用以下代码获取的token返回到android客户端后(客户端安卓,服务器端PHP,客户端先向服务器获取up token,然后凭token上传具体文件),新上传的图片没有覆盖原图。请问是哪儿有问题了呢?
php端:
php端:
$bucket = "phpsdk";
$accessKey = QINIU_ACCESS_KEY;
$secretKey = QINIU_SECRET_KEY;
Qiniu_SetKeys($accessKey, $secretKey);
$key=$this->input->post("file_key");
if($key!=""){
$putPolicy = new Qiniu_RS_PutPolicy($bucket.":".$key);
$upToken = $putPolicy->Token(null);
echo $upToken;
}
android端:
//tmpFile是上传的文件, key是我需要更新的文件名,qiniuUploadToken是获取到的上传凭证
UploadManager uploadManager = new UploadManager();
uploadManager.put(tmpFile, key, qiniuUploadToken, new UpCompletionHandler() {
@Override
public void complete(String key, ResponseInfo info, JSONObject response) {
try {
if(response!=null){
final String actualKey=response.getString("key");
Log.d(TAG, "get server side key = "+actualKey);
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) { }
});
MessageQueue.getInstance().addToRequestQueue(jsonObjReq, TAG);
}else{
Log.e(TAG, "Error from Qiniu: "+info.error);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new UploadOptions(null, null, false, new UpProgressHandler(){
public void progress(String key, double percent){
Log.i("qiniu", key + " (progress): " + percent);
}
}, new UpCancellationSignal(){
public boolean isCancelled(){
return isQiniuCancelled;
}
}));
回复内容:
我希望通过设置key为bucket:key的方式来获取可用于更新的文件token,但是用以下代码获取的token返回到android客户端后(客户端安卓,服务器端PHP,客户端先向服务器获取up token,然后凭token上传具体文件),新上传的图片没有覆盖原图。请问是哪儿有问题了呢?
php端:
$bucket = "phpsdk";
$accessKey = QINIU_ACCESS_KEY;
$secretKey = QINIU_SECRET_KEY;
Qiniu_SetKeys($accessKey, $secretKey);
$key=$this->input->post("file_key");
if($key!=""){
$putPolicy = new Qiniu_RS_PutPolicy($bucket.":".$key);
$upToken = $putPolicy->Token(null);
echo $upToken;
}
android端:
//tmpFile是上传的文件, key是我需要更新的文件名,qiniuUploadToken是获取到的上传凭证
UploadManager uploadManager = new UploadManager();
uploadManager.put(tmpFile, key, qiniuUploadToken, new UpCompletionHandler() {
@Override
public void complete(String key, ResponseInfo info, JSONObject response) {
try {
if(response!=null){
final String actualKey=response.getString("key");
Log.d(TAG, "get server side key = "+actualKey);
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) { }
});
MessageQueue.getInstance().addToRequestQueue(jsonObjReq, TAG);
}else{
Log.e(TAG, "Error from Qiniu: "+info.error);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new UploadOptions(null, null, false, new UpProgressHandler(){
public void progress(String key, double percent){
Log.i("qiniu", key + " (progress): " + percent);
}
}, new UpCancellationSignal(){
public boolean isCancelled(){
return isQiniuCancelled;
}
}));
这个有可能是cdn缓存导致的覆盖上传后,访问到的文件仍然是旧的资源,你可以通过url?v=20150309对资源进行访问,如果是访问到的最新的资源,说明资源覆盖成功了;一般资源的缓存刷新时间是10min开始刷新,24h全网刷新,如果你对资源更新的实时性要求比较高,你可以在空间设置的高级设置中刷新下资源的url,另外也可以采用资源url加上查询参数的方式去访问资源。