当前位置: 首页 > news >正文

萍乡网站seo网页设计职位

萍乡网站seo,网页设计职位,淘宝网站建设素材,营销咨询师招聘golang实现可中断的流式下载 最近有一个需要实现下载功能: 从服务器上读取文件,返回一个ReadCloser在用户磁盘上创建文件,通过io.Copy实现文件下载(io.Copy是流式的操作,不会出现因文件过大而内存暴涨的问题&#xff0…

golang实现可中断的流式下载

最近有一个需要实现下载功能:

  1. 从服务器上读取文件,返回一个ReadCloser
  2. 在用户磁盘上创建文件,通过io.Copy实现文件下载(io.Copy是流式的操作,不会出现因文件过大而内存暴涨的问题)
  3. 通过context实现暂停

1 流式下载:io.Copy

这里拷贝文件我们选择的是io.Copy而非是通过ioutil.ReadAll()将body中返回的数据一次性读取到内存

通过io.Copy可以保证内存占用一直处于一个比较稳定的水平

2 可中断:context

通过封装io.Copy实现

  • 将io.Copy封装为一个方法,方法里传入context,外部通过context.WithCancel()控制流式拷贝的暂停

3 全部代码

这里演示我通过读取S3的一个对象下载到本地


/*通过io.Copy实现可中断的流复制
*/
var (ak       = "99999999999999999999"sk       = "9999999999999999999999999999999999999999"endpoint = "http://xx.xx.xx.xx:8060"bucket   = "test-bucket"key      = "d_xp/2G/2G.txt"
)func main() {s3Client := osg.Client.GetS3Client(ak, sk, endpoint)ctx, cancelFunc := context.WithCancel(context.Background())object, err := s3Client.GetObject(ctx, &s3.GetObjectInput{Bucket: aws.String(bucket),Key:    aws.String(key),})go func() {time.Sleep(time.Second * 10)cancelFunc()log.Infof("canceled...")}()if err != nil {log.Errorf("%v", err)return}body := object.Bodydefer body.Close()file, err := os.Create("/Users/ziyi/GolandProjects/MyTest/demo_home/io_demo/target.txt")if err != nil {log.Errorf("%v", err)return}defer file.Close()_, err = FileService.Copy(ctx, file, body)if err != nil {log.Errorf("%v", err)return}}type fileService struct {sem *semaphore.Weighted
}var FileService = &fileService{sem: semaphore.NewWeighted(1),
}type IoCopyCancelledErr struct {errMsg string
}func (e *IoCopyCancelledErr) Error() string {return fmt.Sprintf("io copy error, %s", e.errMsg)
}func NewIoCopyCancelledErr(msg string) *IoCopyCancelledErr {return &IoCopyCancelledErr{errMsg: msg,}
}type readerFunc func(p []byte) (n int, err error)func (rf readerFunc) Read(p []byte) (n int, err error) { return rf(p) }//通过ctx实现可中断的流拷贝
// Copy closable copy
func (s *fileService) Copy(ctx context.Context, dst io.Writer, src io.Reader) (int64, error) {// Copy will call the Reader and Writer interface multiple time, in order// to copy by chunk (avoiding loading the whole file in memory).// I insert the ability to cancel before read time as it is the earliest// possible in the call process.size, err := io.Copy(dst, readerFunc(func(p []byte) (int, error) {select {// if context has been canceledcase <-ctx.Done():// stop process and propagate "context canceled" errorreturn 0, NewIoCopyCancelledErr(ctx.Err().Error())default:// otherwise just run default io.Reader implementationreturn src.Read(p)}}))return size, err
}
http://www.yayakq.cn/news/296468/

相关文章:

  • 金融理财网站建设吴江公司网站建设电话
  • 大兴做网站公司流行的网站开发技术
  • 坑梓网站建设包括哪些一起做网店一样的网站
  • 网站建设及空间淘宝代运营服务
  • 广州化妆品网站制作广西建设网注册中心
  • 江山网站设计wordpress二次开发手册
  • 建设银行网站银行登录网站设置默认首页
  • wordpress head.php北京网站设计制作关键词优化
  • php网站开发综合案例个体企业建网站
  • wordpress可以做成企业站吗深入解析wordpress 下载
  • 三网合一网站建设是指什么安国市住房和城乡建设局网站
  • 重庆承越网站建设公优书网首页
  • 网站商城建设费用重庆机械加工网
  • wordpress 统计wordpress如何优化页面
  • 网站建设先进个人自荐学技术的培训学校
  • 南充外贸网站建设wordpres做视频网站
  • 自助建站网站公司网站的优化和推广方案怎么写
  • 慈溪做网站建网站软件下载
  • python 做的网站有哪些南京小程序定制开发
  • wordpress网站如何播放视频播放器淮安做网站的有多少
  • 网店怎么开啊流程是怎样北京厦门网站优化
  • 做薆视频网站东营网站制作方案
  • 商丘在线商城东莞seo优化公司
  • 上海网站建设服务市价wordpress mysql版本
  • 株洲网站建设平台汕头网站搜索优化
  • 建筑网站设计模版x站源码免费分享
  • 网站网页设计收费wordpress yusi1.0
  • 我需要做网站怎样优化排名自己网站
  • 上海华东民航机场建设公司网站饰品类网站建设定位
  • 竞价单页网站模板深圳市城乡和建设局网站首页