Location>code7788 >text

Seven Bulls Cloud Private Space Image Uploading and Downloading

Popularity:973 ℃/2024-08-07 21:56:13

navigator

  • introductory
  • general idea
  • Configuration files related to Seven Bulls Cloud
  • Get Seven Bulls Cloud upload token
    • Related Class Definitions
    • Core Code Implementation
  • Get Seven Bulls Cloud image download link
    • open space
    • private space
    • Core Code Implementation
  • concluding remarks
  • consultation

introductory

We're growing and so is the code.

Multimedia images are widely used in various websites, applets and apps, and also greatly enhance the user experience.

With the rise of cloud services, more and more applications are choosing to host their data in the cloud.

The multimedia material on the cloud technology is becoming more mature, not only can reduce costs and increase efficiency, but also bring users an extremely fast experience.

This article focuses on a generic implementation of image file uploading and downloading based on Seven Bulls Cloud in a web/small program project.

general idea

Let's take a look at the process we use to upload and download images in real projects (especially front-end and back-end separations).



The above flow clearly shows the interaction logic between user-client-WebAPI-SevenNiu Cloud.
Among them, we focus on the implementation of the yellow part.

Configuration files related to Seven Bulls Cloud

We uploaded the images to the Seven Bulls Cloud, which involves some Seven Bulls Cloud related configuration.

 // QiNiu KEY
 "QiNiu": {
   "Key": "sevenniu cloudkey",
   "Skey": "sevencowcloudkey",
   "ImgServerUrl":"/",//configure according to the actual
   "DefaultBucket": "PublicSpaceBucketName",//configure according to the actual situation
   "privateUrl": "",//configure according to the reality
   "https": null, ///
   "privateBucket": "privateBucketName",/// Configure as you see fit.
   "publicUrl": "/" // Configure as appropriate.
 }, // "privateBucket": "private space bucketName", // configure as appropriate "publicUrl": "/" // configure as appropriate

The relevant configuration can be viewed by logging into Seven Bulls Cloud.

For example, this is my own AccessKey/SecretKey:


Get Seven Bulls Cloud upload token

Related Class Definitions

/// <summary>
/// Seven Bulls Upload Space Type
/// </summary
public enum FileUploadBucketEnum
{
    /// <summary>
    /// uncharted
    /// </summary>
    [Description("uncharted")]
    UN_KNOW = -1,

    /// <summary>
    /// held in common
    /// </summary>
    [Description("held in common")]
    PUBLIC_BUCKET = 0,
    /// <summary>
    /// private equity
    /// </summary>
    [Description("private equity")]
    PRIVATE_BUCKET = 1
}
/// <summary>
 /// Client passed parameters Distinguish between public and private space
 /// </summary>; /// Client passes parameters to distinguish between public and private space
public class GetQINiuTokenDto
{
    public class GetQINiuTokenDto { public string path; [Required(ErrorMessage)
    [Required(ErrorMessage = "Space type required")]
    public FileUploadBucketEnum bucket = FileUploadBucketEnum.UN_KNOW;
}
 /// <summary>
 /// Parameters returned to the client
 /// </summary>
public class QiniuTokenRsp
 {
     /// <summary>
     /// Token
     /// </summary>
     public string uptoken { get; set; }
     /// <summary>
     /// filename
     /// </summary>
     public string FileName { get; set; }
     
 }

Implementation Code

 /// <summary>
 //// Getting an image upload token for Seven Bulls Cloud
 /// </summary>
 /// <param name="dto"></param>
 /// <returns> </returns>
 public QiniuTokenRsp GetQiNiuToken(GetQINiuTokenDto dto)
 {
     Mac mac = new Mac(["QiNiu:Key"], ["QiNiu:SKey"]);
     PutPolicy putPolicy = new PutPolicy
     PutPolicy = new PutPolicy {
         Scope = == FileUploadBucketEnum.PRIVATE_BUCKET ? ["QiNiu:privateBucket"] : ["QiNiu:DefaultBucket"]
     };
     (3600).
     // Temporarily limit the transfer to images only
      = "image/jpeg;image/png;audio/mpeg;";"; // Temporary backend limit of 20.
     // Temporarily limit the backend to 20 and the frontend to 10, because there's still loading, formatting, cropping, and so on.
      = 1024 * 1024 * 20.

     string jstr = (); string token = (mac, jstr)
     
     var fileKey = $"{}/{:yyyyMMddHHmmss}{()}";

     return new QiniuTokenRsp
     {
         uptoken = token, fileName = fileKey
         FileName = fileKey
     }; }
 }

Get the Seven Bulls Cloud image download link

Many people may not be aware of the difference between public and private space on Seven Bulls Cloud, so here is a brief introduction.

File downloads are categorized into file downloads in public space and file downloads in private space.

open space

For the public space, the link to access is mainly the domain name bound to the space (can be the default domain name of the storage space or the bound custom domain name) spliced on the space inside the file name can be accessed, the standard situation needs to be spliced before the link to the file name for urlencode in order to be compatible with different characters.

string domain = "";
string key = "hello/world/sevencow/"; string publicUrl = (domain, key); string
string publicUrl = (domain, key).
(publicUrl);

private space

For private space, you first need to construct a corresponding public space access link according to the public space file access method, and then sign this link with private authorization.

Mac mac = new Mac(AccessKey, SecretKey);
string domain = "";
string key = "hello/world/Seven Bulls (PRC appliance brand)/";
string privateUrl = (mac, domain, key, 3600);
(privateUrl);

Core Code Implementation

On the basis of the above official interface, I did a little packaging, the code is as follows:

/// <summary>
/// Private Space Full Image Download Address
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static String GetPrivateUrl(string key)
{
    String privateUrl = ;
    try
    {
        if ((key))
        {
            return privateUrl;
        }
        Mac mac = new Mac(["QiNiu:Key"], ["QiNiu:SKey"]);
        //Open Space Domains
        string domain = ["QiNiu:privateUrl"];
        privateUrl = (mac, domain, key, 3600);
        //(privateUrl);
    }
    catch (Exception ex)
    {
        ($":{}");
    }
    return privateUrl;
}
/// <summary>
/// Public Space Full Image Download Address
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static String GetPublicUrl(string key)
{
    String publicUrl = ;
    try
    {
        if ((key))
        {
            return publicUrl;
        }
        Mac mac = new Mac(["QiNiu:Key"], ["QiNiu:SKey"]);
        string domain = ["QiNiu:publicUrl"];
        publicUrl = (domain, key);
    }
    catch (Exception ex)
    {
        ($":{}");
    }
    return publicUrl;
}

concluding remarks

Recently, I received an urgent need to migrate the upload path of business license and other related qualification documents from public space to private space.

This one involved revamping some old projects, and in this revamping further deepened my understanding of the Seven Bulls Cloud upload and download ideas.

Here the author only according to personal work experience, a little thinking and sharing, throwing bricks to attract jade, welcome everyone afraid to criticize and correct.

2024.08.07

Chengdu

consultation

  • Seven Bulls Cloud C# SDK
  • Springboot Integration of Seven Bulls Cloud Image Uploading and Downloading
  • "Springboot real-life documentary source code".