rails ActiveStorage 的 blob 表中的校验和是如何计算的 [英] How is the checksum calculated in the blobs table for rails ActiveStorage

查看:28
本文介绍了rails ActiveStorage 的 blob 表中的校验和是如何计算的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道在 Rails 5.2+ 上使用 ActiveStorage 时如何计算 active_storage_blobs 中的校验和字段?

Does anyone know how the checksum field in active_storage_blobs is calculated when using ActiveStorage on rails 5.2+?

对于奖励积分,有谁知道我如何让它使用与 md5 CLI 命令中的校验和匹配的 md5 校验和?

For bonus points, does anyone know how I can get it to use an md5 checksum that would match the one from the md5 CLI command?

推荐答案

让我们分解一下

我知道我参加聚会有点晚了,但这更适合那些在寻找答案时遇到此问题的人.所以这里是..

Lets Break It Down

I know i'm a bit late to the party, but this is more for those that come across this in a search for answers. So here it is..

背景:

Rails 在 5.2 版中引入了大量新功能,其中之一是 ActiveStorage.官方最终版本于 2018 年 4 月 9 日发布.

Rails introduced loads of new features in version 5.2, one of which was ActiveStorage. The official final release came out on April 9th, 2018.

免责声明:

所以要完全清楚,以下信息与开箱即用的vanilla 活动存储有关.这也没有考虑到一些围绕某些一次性场景的疯狂代码.

So to be perfectly clear, the following information pertains to out-of-the-box vanilla active storage. This also doesn't take into account some crazy code-fu that revolves around some one off scenario.

话虽如此,校验和的计算方式取决于您的 Active Storage 设置.使用原版开箱即用的 Rails Active Storage,有 2 种类型"(缺乏更好的术语)配置.

With that said, the checksum is calculated differently depending on your Active Storage setup. With the vanilla out-of-the-box Rails Active Storage, there are 2 "types" (for lack of a better term) of configuration.

  1. 代理上传
  2. 直接上传

<小时>

代理上传

文件上传流程: [客户] →[RoR App] →[存储服务]


Proxy Uploads

File Upload Flow: [Client] → [RoR App] → [Storage Service]

通讯.流程:可能会有所不同,但在大多数情况下,它应该类似于文件上传流程.

Comm. Flow: Can vary but in most cases it should be similar to File upload flow.

上面在 SparkBao 的回答中指出的是代理上传".这意味着您将文件上传到 RoR 应用程序并执行某种处理,然后再将文件发送到您配置的存储服务(AWS、Azure、Google、BackBlaze 等).即使您将存储服务设置为localdisk",该逻辑在技术上仍然适用,即使您的 RoR 应用程序是存储端点.

Pointed out above in SparkBao's answer is a "Proxy Upload". Meaning you upload the file to your RoR application and perform some sort of processing before sending the file to your configured storage service (AWS, Azure, Google, BackBlaze, etc...). Even if you set your storage service to "localdisk" the logic still technically applies, even though your RoR application is the storage endpoint.

代理上传"方法对于在 Heroku 等服务上部署在云中的 RoR 应用程序并不理想.Heroku 有 30 秒的硬性限制来完成您的交易并将响应发送回您的客户端(最终用户).因此,如果您的文件相当大,则需要考虑文件上传所需的时间,然后考虑计算校验和的时间.如果您遇到无法在 30 秒内通过响应完成请求的情况,您将需要使用直接上传"方法.

A "Proxy Upload" approach isn't ideal for RoR applications that are deployed in the cloud on services like Heroku. Heroku has a hardset limit of 30 seconds to complete your transaction and send a response back to your client (end user). So if your file is fairly large, you need to consider the time it takes for your file to upload, and then account for the amount of time to calculate the checksum. If your caught in a scenario where you can't complete the request with a response in the 30 seconds you will need to use the "Direct Upload" approach.

代理上传答案:

Ruby 类 Digest::MD5 用于方法 compute_checksum_in_chunks(io) 正如 Spark.Bao 指出的那样.

The Ruby class Digest::MD5 is used in the method compute_checksum_in_chunks(io) as pointed out by Spark.Bao.

文件上传流程: [客户] →[存储服务]

File Upload Flow: [Client] → [Storage Service]

通讯.流程: [客户] →[RoR App] →[客户] →[仓储服务] →[客户] →[RoR App] →[客户]

Comm. Flow: [Client] → [RoR App] → [Client] → [Storage Service] → [Client] → [RoR App] → [Client]

我们维护和开发 Rails 的好朋友已经为我们完成了所有繁重的工作.我不会详细介绍如何设置直接上传,但这里有一个关于如何 » 的链接.Rails EdgeGuide - 直接上传.

Our fine friends that maintain and develop Rails have already done all the heavy lifting for us. I won't go into details on how to setup a direct upload, but here is a link on how » Rails EdgeGuide - Direct Uploads.

代理上传答案:

现在说了这么多,有了开箱即用的直接上传"设置,文件校验和是通过利用 SparkMD5 (JavaScript).

Now with all that said, with a vanilla out-of-the-box "Direct Uploads" setup, a file checksum is calculated by leveraging SparkMD5 (JavaScript).

以下是的片段Rails 主动存储源代码- (activestorage.js)

  var fileSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;
  var FileChecksum = function() {
    createClass(FileChecksum, null, [ {
      key: "create",
      value: function create(file, callback) {
        var instance = new FileChecksum(file);
        instance.create(callback);
      }
    } ]);
    function FileChecksum(file) {
      classCallCheck(this, FileChecksum);
      this.file = file;
      this.chunkSize = 2097152;
      this.chunkCount = Math.ceil(this.file.size / this.chunkSize);
      this.chunkIndex = 0;
    }
    createClass(FileChecksum, [ {
      key: "create",
      value: function create(callback) {
        var _this = this;
        this.callback = callback;
        this.md5Buffer = new sparkMd5.ArrayBuffer();
        this.fileReader = new FileReader();
        this.fileReader.addEventListener("load", function(event) {
          return _this.fileReaderDidLoad(event);
        });
        this.fileReader.addEventListener("error", function(event) {
          return _this.fileReaderDidError(event);
        });
        this.readNextChunk();
      }
    },

如果有什么我遗漏的地方,我会提前道歉.我尽量做到彻底.

If there is anything I missed I do apologize in advance. I tried to be as thorough as possible.

所以总结一下以下应该足以作为可接受的答案:

So to Sum things up the following should suffice as an acceptable answer:

直接上传配置: JavaScript 哈希库 SparkMD5.

这篇关于rails ActiveStorage 的 blob 表中的校验和是如何计算的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆