oss和ossclient上传区别
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oss和ossclient上传区别相关的知识,希望对你有一定的参考价值。
参考技术A 1、oss是阿里云对象存储服务是阿里云提供的海量、安全、低成本、高可靠的云存储服务,它是一个分布式的对象存储服务,提供的是一个Key-Value对形式的对象存储服务。2、OSSClient是OSS的Java客户端,用于管理存储空间和文件等OSS资源。
3、oss和ossclient作用不同,意思不同。
OSS 上传下载的进度条
ossClient.uploadFile和ossClient.downloadFile方法不支持进度条功能.
根据官方文档 https://help.aliyun.com/document_detail/84796.html?spm=a2c4g.11186623.6.791.40554d83cDiWSN
流式上传不支持百分比 而new File是支持的
流式上传:
Start to upload...... 8192 bytes have been written at this time, upload ratio: unknown(8192/...) 8192 bytes have been written at this time, upload ratio: unknown(16384/...) 8192 bytes have been written at this time, upload ratio: unknown(24576/...) 8192 bytes have been written at this time, upload ratio: unknown(32768/...) 8192 bytes have been written at this time, upload ratio: unknown(40960/...) 8192 bytes have been written at this time, upload ratio: unknown(49152/...) 8192 bytes have been written at this time, upload ratio: unknown(57344/...) 1967 bytes have been written at this time, upload ratio: unknown(59311/...) Succeed to upload, 59311 bytes have been transferred in total
而new File(localFilePath);是可以看到百分比的,从开始也获取了文件的整个大小.
Start to upload...... 3546429 bytes in total will be uploaded to OSS 8192 bytes have been written at this time, upload progress: 0%(8192/3546429) 8192 bytes have been written at this time, upload progress: 0%(16384/3546429) 8192 bytes have been written at this time, upload progress: 0%(24576/3546429) 8192 bytes have been written at this time, upload progress: 0%(32768/3546429) 8192 bytes have been written at this time, upload progress: 1%(40960/3546429) 8192 bytes have been written at this time, upload progress: 1%(49152/3546429) 8192 bytes have been written at this time, upload progress: 1%(57344/3546429) .... .... 7485 bytes have been written at this time, upload progress: 100%(3546429/3546429) Succeed to upload, 3546429 bytes have been transferred in total
整个上传下载速度都还挺快的
试了下300MB文件每次2M/s
上传监听器
package com.springboot.oss.listener; import com.aliyun.oss.event.ProgressEvent; import com.aliyun.oss.event.ProgressEventType; import com.aliyun.oss.event.ProgressListener; /** * ossClient.putObject, ossClient.getObject, ossClient.uploadPart方法支持进度条功能. * ossClient.uploadFile和ossClient.downloadFile方法不支持进度条功能. * 简单上传进度条监听器 * https://help.aliyun.com/document_detail/84796.html?spm=a2c4g.11186623.6.791.40554d83cDiWSN */ public class PutObjectProgressListener implements ProgressListener { private long bytesWritten = 0; private long totalBytes = -1; private boolean succeed = false; @Override public void progressChanged(ProgressEvent progressEvent) { long bytes = progressEvent.getBytes(); ProgressEventType eventType = progressEvent.getEventType(); switch (eventType) { case TRANSFER_STARTED_EVENT: System.out.println("Start to upload......"); break; case REQUEST_CONTENT_LENGTH_EVENT: this.totalBytes = bytes; System.out.println(this.totalBytes + " bytes in total will be uploaded to OSS"); break; case REQUEST_BYTE_TRANSFER_EVENT: this.bytesWritten += bytes; if (this.totalBytes != -1) { int percent = (int)(this.bytesWritten * 100.0 / this.totalBytes); System.out.println(bytes + " bytes have been written at this time, upload progress: " + percent + "%(" + this.bytesWritten + "/" + this.totalBytes + ")"); } else { System.out.println(bytes + " bytes have been written at this time, upload ratio: unknown" + "(" + this.bytesWritten + "/...)"); } break; case TRANSFER_COMPLETED_EVENT: this.succeed = true; System.out.println("Succeed to upload, " + this.bytesWritten + " bytes have been transferred in total"); break; case TRANSFER_FAILED_EVENT: System.out.println("Failed to upload, " + this.bytesWritten + " bytes have been transferred"); break; default: break; } } public boolean isSucceed(){ return succeed; } }
下载监听器
package com.springboot.oss.listener; import com.aliyun.oss.event.ProgressEvent; import com.aliyun.oss.event.ProgressEventType; import com.aliyun.oss.event.ProgressListener; /** * 文件下载监听器 * ossClient.uploadFile和ossClient.downloadFile方法不支持进度条功能. */ public class GetObjectProgressListener implements ProgressListener { private long bytesRead = 0; private long totalBytes = -1; private boolean succeed = false; @Override public void progressChanged(ProgressEvent progressEvent) { long bytes = progressEvent.getBytes(); ProgressEventType eventType = progressEvent.getEventType(); switch (eventType) { case TRANSFER_STARTED_EVENT: System.out.println("Start to download......"); break; case RESPONSE_CONTENT_LENGTH_EVENT: this.totalBytes = bytes; System.out.println(this.totalBytes + " bytes in total will be downloaded to a local file"); break; case RESPONSE_BYTE_TRANSFER_EVENT: this.bytesRead += bytes; if (this.totalBytes != -1) { int percent = (int)(this.bytesRead * 100.0 / this.totalBytes); System.out.println(bytes + " bytes have been read at this time, download progress: " + percent + "%(" + this.bytesRead + "/" + this.totalBytes + ")"); } else { System.out.println(bytes + " bytes have been read at this time, download ratio: unknown" + "(" + this.bytesRead + "/...)"); } break; case TRANSFER_COMPLETED_EVENT: this.succeed = true; System.out.println("Succeed to download, " + this.bytesRead + " bytes have been transferred in total"); break; case TRANSFER_FAILED_EVENT: System.out.println("Failed to download, " + this.bytesRead + " bytes have been transferred"); break; default: break; } } public boolean isSucceed() { return succeed; } }
关于怎样把进度展示到前端去,可能需要在方法调用上再调整一番.
以上是关于oss和ossclient上传区别的主要内容,如果未能解决你的问题,请参考以下文章