android oss 简书,迷之自信的对象存储 OSS SDK陷入无限等待

阿里的对象存储 OSS SDK 对于自己的网络可能是特别的自信,sdk中超时时间是-1,也就是不设置超时时间。写这个sdk的作者是认为不可能超时,推送一定能够成功到达么?

如果遇到网络原因,可能会处于无限等待中,将导致整个程序无反应。

而MSDN中HttpWebRequest的默认超时时间是100秒。

5cf6c7dc916e

本来我用的多线程传输图片,结果一个个的等待,所有线程都处于等待状态中,程序没反应,也不报错,最近一段时间比较频繁。

public class ClientConfiguration: ICloneable

{

...

private int _connectionTimeout = -1;//默认为-1

...

}

internal static class HttpFactory

{

...

// Set request headers

private static void SetRequestHeaders(HttpWebRequest webRequest, ServiceRequest serviceRequest,

ClientConfiguration configuration)

{

webRequest.Timeout = configuration.ConnectionTimeout;//默认-1

webRequest.ReadWriteTimeout = configuration.ConnectionTimeout;//默认-1

webRequest.Method = serviceRequest.Method.ToString().ToUpperInvariant();

// Because it is not allowed to set common headers

// with the WebHeaderCollection.Add method,

// we have to call an internal method to skip validation.

foreach (var h in serviceRequest.Headers)

{

// Nginx does not accept a chunked encoding request with Content-Length, as detailed in #OSS-2848

if (h.Key.Equals(HttpHeaders.ContentLength) && (serviceRequest.UseChunkedEncoding ||

(serviceRequest.Content != null && !serviceRequest.Content.CanSeek) || serviceRequest.Content == null))

{

continue;

}

HttpExtensions.AddInternal(webRequest.Headers, h.Key, h.Value);

}

// Set user-agent

if (!string.IsNullOrEmpty(configuration.UserAgent))

webRequest.UserAgent = configuration.UserAgent;

}

...

}

解决这个问题的方法很简单,在实例化时,把超时的时间修改一下就好了,代码如下:

var _configuration = new ClientConfiguration();

_configuration.ConnectionTimeout = 20000; //单位是毫秒,设置20秒超时

var _client = new OssClient(endpoint, accessKeyId, accessKeySecret, _configuration);

public class GetAndUploadFileDemo { private static String TAG = "GetAndUploadFileDemo"; private OSSService ossService; private OSSBucket bucket; public void show() { ossService = OSSServiceProvider.getService(); bucket = ossService.getOssBucket("youcaidao"); // 文件的常规操作如普通上传、下载、拷贝、删除等,与Data类一致,故这里只给出断点下载和断点上传的demo resumableDownloadWithSpecConfig(); // delay(); // resumableUpload(); // delay(); // resumableDownload(); // delay(); } public void delay() { try { Thread.sleep(30 * 1000); } catch (Exception e) { e.printStackTrace(); } } // 断点上传 public void resumableUpload() { // OSSData ossData = ossService.getOssData(sampleBucket, "sample-data"); // ossData.setData(data, "raw"); // 指定需要上传的数据和它的类型 // ossData.enableUploadCheckMd5sum(); // 开启上传MD5校验 // ossData.upload(); // 上传失败将会抛出异常 OSSFile bigfFile = ossService.getOssFile(bucket, "de.jpg"); try { bigfFile.setUploadFilePath( "/storage/emulated/0/Android/data/com.qd.videorecorder/video/VMS_1439866564822.jpg", "image/jpg"); bigfFile.ResumableUploadInBackground(new SaveCallback() { @Override public void onSuccess(String objectKey) { Log.d(TAG, "[onSuccess] - " + objectKey + " upload success!"); } @Override public void onProgress(String objectKey, int byteCount, int totalSize) { Log.d(TAG, "[onProgress] - current upload " + objectKey + " bytes: " + byteCount + " in total: " + totalSize); } @Override public void onFailure(String objectKey, OSSException ossException) { Log.e(TAG, "[onFailure] - upload " + objectKey + " failed!\n" + ossException.toString()); ossException.printStackTrace(); ossException.getException().printStackTrace(); } }); } catch (FileNotFoundException e) { e.printStackTrace(); } } // 断点下载 public void resumableDownload() { OSSFile bigFile = ossService.getOssFile(bucket, "bigFile.dat"); bigFile.ResumableDownloadToInBackground( "/storage/sdcard0/src_file/bigFile.dat", new GetFileCallback() { @Override public void onSuccess(String objectKey, String filePath) { Log.d(TAG, "[onSuccess] - " + objectKey + " storage path: " + filePath); } @Override public void onProgress(String objectKey, int byteCount, int totalSize) { Log.d(TAG, "[onProgress] - current download: " + objectKey + " bytes:" + byteCount + " in total:" + totalSize); } @Override public void onFailure(String objectKey, OSSException ossException) { Log.e(TAG, "[onFailure] - download " + objectKey + " failed!\n" + ossException.toString()); ossException.printStackTrace(); } }); } // 设置相关参数的断点续传 public void resumableDownloadWithSpecConfig() { OSSFile bigFile = ossService .getOssFile(bucket, "VMS_1439866564822.jpg"); ResumableTaskOption option = new ResumableTaskOption(); option.setAutoRetryTime(2); // 默认为2次,最大3次 option.setThreadNum(2); // 默认并发3个线程,最大5个 bigFile.ResumableDownloadToInBackground( "/storage/emulated/0/Android/data/com.qd.videorecorder/video/VMS_1439866564822.jpg", new GetFileCallback() { // /storage/emulated/0/DCIM/Camera/VID_20150803_173350.mp4 @Override public void onSuccess(String objectKey, String filePath) { System.out.println("[onSuccess] - " + objectKey + " storage path: " + filePath); } @Override public void onProgress(String objectKey, int byteCount, int totalSize) { System.out.println("[onProgress] - current download: " + objectKey + " bytes:" + byteCount + " in total:" + totalSize); } @Override public void onFailure(String objectKey, OSSException ossException) { System.out.println("[onFailure] - download " + objectKey + " failed!\n" + ossException.toString()); ossException.printStackTrace(); } }); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值