When uploading the APK function, I found a problem, about whether the files uploaded to Qiniu can be overwritten. The following is the conclusion of referring to the documents and Qiniu students

Whether the file with the same name is overwritten

When the server side generates the token, we write it like this

const policyParams = { scope: bucket }

const putPolicy = new qiniu.rs.PutPolicy(policyParams)

const uploadToken = putPolicy.uploadToken(mac)

return uploadToken

You can see that setting the PolicyParams object is the first step in generating the Token

There are actually a number of other keys in PolicyParams that you can look at in the Qigniu documentation

The two keys that are involved in whether a file is overwritten are Scope and InsertOnly

The explanation of scope parameter in seven cows

Specify the target resource space Bucket and resource Key (up to 750 bytes) for uploading:

There are three formats


  1. , which allows users to upload files to the specified bucket. In this format, the file can only be added (insertOnly 1 is required for the sharded upload). If the resource with the same name exists (and the file content /etag is inconsistent), the upload will fail. If the content /etag of the existing resource is the same, the upload will return successful.

  2. :

    :

    In this format, files are allowed to be modified by default, and resources with the same name will be overwritten if they already exist. If you want to upload only the file with the specified key and no modifications are allowed, you can set the following insertOnly attribute value to 1.

  3. :

    allows uploading of files specified with the keyPrefix prefix, if and only if the isPrefixalScope field is 1.

In our project, we use the first one, according to the logic of the first one:

If the resource with the same name already exists but the content is inconsistent, the upload will fail. If a resource with the same name already exists, the upload will return success

Local testing found that if two images with the same name but different content uploaded, the second time will fail

Then we tested two APK packages with the same name but different contents, and found that the second upload prompt was successful

Why are the same name of the file but the content is different, seven cattle upload the results are not the same

INSERTONLY = 1 for a sharded upload, or it is an overwrite upload

According to the result, it can be inferred that when APK uploads are carried out, sharded uploads should be used, so the coverage mode is adopted

However, there is no configuration in the project for whether to use sharded uploads

After consulting the students of Qiniu, I replied that the cut-off point for Qiniu file uploading is 4M, and the default for file-sharing uploading is larger than 4M and the default for form uploading is smaller than 4M

In this test, APK is already above 4M, so shardable upload is used by default. Meanwhile, INSERTONLY is not set in policyParams parameter, which results in the use of overwriting upload policy. Therefore, the second APK with the same name can be uploaded successfully

Why the same name of the file after overwriting, click the link to view the original file

After the second upload of a large file, use the original link to download the file, you will find that the file content has not been updated.

But it did work the second time

Qiniu replied that the CDN cache should be updated. That is to say, even if the file is updated successfully, if the CDN cache is not updated actively through the interface, then the file content that users see through the link is also the old version of the file

Supplement:

  • Check the seven cattle file content is consistent judgment

Seven cattle determine whether the file content is consistent, https://github.com/qiniu/qetag/blob/master/qetag.js by this function can generate a etag string

`

GetEtag (‘. / xxx_6. 10.2 _a1_test. Apk ‘, etags = > {

  console.log(‘origin1’, etags)

})

GetEtag (‘. / same/xxx_6. 10.2 _a1_test. Apk ‘, etags = > {

  console.log(‘same111’, etags)

})

GetEtag (‘. / copy/xxxx_6. 10.2 _a1_test. Apk ‘, etags = > {

  console.log(‘origin2’, etags)

}) `

Qiniu judges whether the file content is consistent by comparing the generated ETag

  • Two Nouns
  • Overwrite uploads, whether previously or not, that read the last result
  • New, is only successful for the first time, after all failures