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

四川建设行业网站有哪些网站建设哪家好 需要多少钱

四川建设行业网站有哪些,网站建设哪家好 需要多少钱,建立网站账号违法行为数据库,创建网站如何注册背景 我们的项目每天都会并行上传好几万份文件到下游的GCP Cloud Storage#xff0c;当文件比较大时#xff0c;会采用GCP的可续上传方案#xff0c;通过把文件切分成多个数据块#xff0c;分多次HTTP请求上传到GCP Bucket#xff0c;具体可参考https://cloud.google.com…背景 我们的项目每天都会并行上传好几万份文件到下游的GCP Cloud Storage当文件比较大时会采用GCP的可续上传方案通过把文件切分成多个数据块分多次HTTP请求上传到GCP Bucket具体可参考https://cloud.google.com/storage/docs/performing-resumable-uploads。 但是在实际应用中会发现文件比较大时由于数据包会被分成多次HTTP请求上传偶尔GCP会返回400错误码导致上传失败但是查看各个请求参数都属于正常目前不确定GCP一些网络限制导致还是该 API 存在性能问题。于是我选择使用另一种替代方案尝试使用以下方式通过GCP 官方SDK进行文件上传。 GoogleCredentials apiCredentials GoogleCredentials.fromStream(new FileInputStream(jsonKeyPath)).createScoped(scopes);Storage storage StorageOptions.newBuilder().setCredentials(apiCredentials).build().getService();BlobId blobId BlobId.of(bucketName, objectName);BlobInfo blobInfo BlobInfo.newBuilder(blobId).setContentType(application/json;charsetUTF-8).setMd5(checksum).build();byte[] bytes Files.readAllBytes(sourceFIle.toPath());storage.create(blobInfo,bytes);SDK API参考https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview。 文件小的时候速度还是比较快因为它是直接Upload不做数据分块但是当文件大点或者数量多点就会出现如下异常 java.lang.OutOfMemoryError: Required array size too large原因是这句代码Files.readAllBytes(sourceFIle.toPath())会一次过把文件直接加载到内存当文件比较大或者文件数量很多的时候就会直接导致内存溢出。 虽然官方SDK也有针对大文件上传提供了可续上传方案例如: String bucketName my-unique-bucket;String blobName my-blob-name;BlobId blobId BlobId.of(bucketName, blobName);byte[] content Hello, World!.getBytes(UTF_8);BlobInfo blobInfo BlobInfo.newBuilder(blobId).setContentType(text/plain).build();try (WriteChannel writer storage.writer(blobInfo)) {writer.write(ByteBuffer.wrap(content, 0, content.length));} catch (IOException ex) {// handle exception}WriteChannel底层原理也是会在传输的时候会分块上传并且遇到网络波动导致传输失败会自动基于上一次传输的数据包进行重传但是这个也只是解决大文件上传性能问题并没办法解决我们在加载源文件就已经内存溢出。 解决方案 其实解决办法也很简单我们只需要把每次读书源文件的数据包在可控范围即可也就是分块读取分块上传这样既保证大文件上传效率也不会因为一次过加载文件太多导致内存溢出。 以下是示范代码 主程序入口: 以下scope对应的是我们所需要的权限https://cloud.google.com/storage/docs/authentication public static void main(String[] args) throws IOException {ListString scopes new ArrayList(Arrays.asList(https://www.googleapis.com/auth/devstorage.full_control,https://www.googleapis.com/auth/devstorage.read_write));//GCP Json Key PathString jsonKeyPath ;//File used to be uploadedFile sourceFile new File(filePath);//Calculate file checksumString checksum DigestUtils.md5Hex(new FileInputStream(sourceFile));//GCS BucketNameString bucketName ;//GCS Target ObjectNameString objectNamesourceFile.getName();//Prepare connection details//https://cloud.google.com/storage/docs/authenticationGoogleCredentials apiCredentials GoogleCredentials.fromStream(new FileInputStream(jsonKeyPath)).createScoped(scopes);Storage storage StorageOptions.newBuilder().setCredentials(apiCredentials).build().getService();BlobId blobId BlobId.of(bucketName, objectName);BlobInfo blobInfo BlobInfo.newBuilder(blobId).setContentType(application/json;charsetUTF-8).setMd5(checksum).build();uploadToBucket(storage, sourceFile, blobInfo);}上传方法: 这里的文件大小需要根据自己服务器实际的内存和带宽去设置一般小文件使用直接上传方法效果最好如果文件比较大就需要进行数据包拆分。 关于数据块大小设置以下是GCP的建议: The chunk size should be a multiple of 256 KiB (256 x 1024 bytes), unless its the last chunk that completes the upload. Larger chunk sizes typically make uploads faster, but note that theres a tradeoff between speed and memory usage. Its recommended that you use at least 8 MiB for the chunk size. public static void uploadToBucket(Storage storage, File sourceFIle, BlobInfo blobInfo) throws IOException {//For small files, we can upload the file in one go// less than 10 MBif(sourceFIle.length() 10000000){byte[] bytes Files.readAllBytes(sourceFIle.toPath());storage.create(blobInfo,bytes);return;}//For big files , we need to split it into multiple chunkstry(WriteChannel writer storage.writer(blobInfo)){//Dont read the whole file because it will cause OutOfMemory issuebyte[] buffer new byte[10240];try(InputStream input Files.newInputStream(sourceFIle.toPath())){int limit;while((limit input.read(buffer))0){writer.write(ByteBuffer.wrap(buffer,0,limit));}}}}以下这一句是解决这个问题的关键每次只读取一部分数据所以不会导致内存溢出。 while((limit input.read(buffer))0){测试效果 这里使用了项目用的GCP Storage和账号进行测试多线程并行去上传4个210MB文件由于我电脑在中国网络而对应GCP Bucket部署在欧洲所以速度不算快。 总结 其实这里解决方案不只适用于谷歌云以前我们使用阿里云的时候上传一些大文件也是采用这样的方式适用于所有的文件传输场景这里其实就是利用分治思想去解决问题。 完整代码 https://github.com/EvanLeung08/cloud-solutions/tree/main/gcs-largefile-upload
http://www.hkea.cn/news/14568850/

相关文章:

  • 个人做网站怎么备案广州网站建设骏域网站
  • 手机网站开发哪个好wordpress右键插件
  • 有后台的网站模板wordpress 搭建界面
  • 做ppt素材的网站什么是广告艺术设计
  • 高端网站开发价格广州城市建设档案网站
  • 在线制作网站宣传视频电商网站开发定制
  • 邮箱注册网站查询网页的优化
  • 北京网站开发费用个人建设网站成本
  • 网站建设开发全包排版设计说明
  • 做企业网站首页尺寸wordpress 文章标题
  • 网站上的flv视频看不了生鲜网站策划
  • 网站建设修饰商品无极在线网站播放
  • 记事本做网站背景我要登录百度
  • 广州微网站建设哪家好深圳知名设计公司有哪些
  • 广州网站优化招聘项目网站建设方案
  • 枣庄手机网站建设公司哈尔滨网站建设2017
  • 做外贸c2c网站有哪些中石化工建设宁波分公司网站
  • 威海那家做网站好媒体电商概念
  • 网站备案中打不开在一个网站下建设多个子网站
  • 网站推广工作好做吗网站指向邮箱超链接怎么做
  • 开发网站需要多少人快速提高网站排名
  • 做爰网站1000部php做商品网站
  • 做个购物网站多少钱电子商务营销策划方案
  • 腾讯公司网页淘宝seo关键词的获取方法有哪些
  • 国家排污许可网站台账怎么做p2p理财网站开发框架
  • 建设工程敎育那个网站wordpress添加微信微博等小工具
  • 建设网站需要哪些编程wordpress关闭rss
  • 合肥做网站的公视频网站开发的视频放在哪
  • 杭州建立网站h5美食制作网站模板下载
  • 旅游电子商务网站建设宁波建站