Make writing a habit together! This is my first day to participate in the “Gold Digging Day New Plan · April More text challenge”, click to see the details of the activity.

Hello ~ I’m Milo! I am building an open source interface testing platform from 0 to 1, and also writing a set of corresponding tutorials, I hope you can support more. Welcome to follow my public account Milo test diary, get the latest article tutorial!

We haven’t seen each other for a long time, and we have the audacity to participate in the wen plan again! I think today we’re going to talk about drawing a bed.

All evil anti-theft chain

About a few weeks ago, the largest github website in China turned on anti-theft mode. What is anti-theft chain? This introduces the concept of the bed below before we prevent pilfer chain, we are certainly not strange to the map bed.

When I have a local image and I want to send it to someone else, what they will see is not my local data, it must be static resources belonging to a site or base64 encoded image data. But the latter is expensive, slightly larger image data will be large.

Then we have the concept of graph bed. Graph bed simply means storing the pictures uploaded by users and providing URL access to the pictures. Won’t this solve the problem that our local pictures can’t be shown to people? In particular, we write articles, often a lot of pictures, if there is a stable graph bed, then the picture in our article content can be replaced by the URL provided by the graph bed (we all know that the URL is unique).

So many sites such as Wordfinch, Nuggets and so on will have their own graph bed, but they tend to limit the use of users. Because do not limit, not only to be mercilessly white piao, but also will be consumed a lot of flow resources.

So anti-theft chain technology emerged, simply said it is to let users only in the corresponding site within the scope of the service domain name or white list domain name to see the corresponding picture, for other sites, all 403. For example, let me just copy the picture of Jane’s book:

It can be seen that will be banned, now anti-theft chain has become a trend. Until a few months ago gi ee could get laid for free, now they have banned it.

Gitee graph bed is added anti-theft chain? I redirected the image 302 to the default icon of Gitee

Where to go

So there is no chart bed that can continue to white whoring, or clearly support your white whoring chart bed!! Of course there is, that is seven niuyun.

(Although any one that advertises free forever ends up charging for it, it seems to me he’s been saying it for years, and at least he’s been explicitly supporting free use.)

Seven niuyun has perfect object storage, 10 G capacity +10 G flow (I believe small sites almost not used up), can also prevent theft chain. But it has a fatal drawback: it requires a domain name!!

Otherwise, it will give you a domain name with an irregular url that will be automatically reclaimed after 30 days.

But we get a domain name is not minute thing, as long as do not buy COM, CN domain name, price we can accept. Recommend everybody can go to domestic bigger cloud server manufacturer to see, look for a proper domain name, I spent 200 or so ocean to buy a domain name of 10 years, calculate rise also is quite cost-effective. (If this seven cattle also don’t let white piao that DT!)

Then today we study how to play seven niuyun!

Step 1: Buy a domain name

If you just want to experiment, you can skip it.

Step 2: Register a Qiniu account and apply for storage space

The official website everybody looks for it by himself, not difficult. Go to the console to request storage space, remember to select private here.

In the absence of a domain name binding, we will only have a temporary domain name. So conditional friends or use their own domain name.

Here binding domain name will have the corresponding tutorial, we click in to follow the operation on the line.

Step 3: Get your token/secret_key

Enter the key Management page to find your own key:

Don’t forget to add your own yo ~

Step 4: Find the corresponding SDK

Using Python as an example, we can find the corresponding Python SDK address

There are more SDK options for languages on the left.

Then follow the API provided by the SDK to complete the IMAGE CRUD.


Using Python as an example, let’s do the following:

  • Install qiniu
pip3 install qiniu
Copy the code
  • Upload a file (through the file path)
# -*- coding: utf-8 -*-
# flake8: noqa
from qiniu import Auth, put_file, etag
import qiniu.config
You need to fill in your Access Key and Secret Key
access_key = 'Access_Key'
secret_key = 'Secret_Key'
Create an authentication object
q = Auth(access_key, secret_key)
# Space to upload
bucket_name = 'Bucket_Name'
The name of the file to save after uploading
key = 'my-python-logo.png'
Generate upload Token, can specify expiration time, etc
token = q.upload_token(bucket_name, key, 3600)
The local path to upload the file
localfile = './sync/bbb.jpg'
ret, info = put_file(token, key, localfile, version='v2') 
print(info)
assert ret['key'] == key
assert ret['hash'] == etag(localfile)
Copy the code
  • Upload a file (via stream)
from qiniu import Auth, put_stream, etag
import qiniu.config
You need to fill in your Access Key and Secret Key
access_key = 'Access_Key'
secret_key = 'Secret_Key'
Create an authentication object
q = Auth(access_key, secret_key)
# Space to upload
bucket_name = 'Bucket_Name'
filepath = "Target file path such as /home/zhangsan.png"
token = q.upload_token(bucket_name, key, 3600)
file_name = os.path.basename(filepath)
Open (file) as stream
ret, info = put_stream(token, key, stream, file_name, len(content))
if ret['key'] != key:
    raise Exception("Upload failed")
Copy the code
  • Update file

    Similar to upload code, there are only upload and download concepts in OSS, updates are overridden by default.

  • Delete the file

# -*- coding: utf-8 -*-
# flake8: noqa
from qiniu import Auth
from qiniu import BucketManager
access_key = 'Access_Key'
secret_key = 'Secret_Key'
Initialize the Auth state
q = Auth(access_key, secret_key)
Initialize the BucketManager
bucket = BucketManager(q)
# the space you want to test, and the key exists in your space
bucket_name = 'Bucket_Name'
key = 'python-logo.png'
# delete file key from bucket_name
ret, info = bucket.delete(bucket_name, key)
print(info)
assert ret == {}
Copy the code
  • The download file

    In fact, most of the time, we do not need to download the file data, the download provided by Seven cows is also a package url for us to download, we can generally use the following way to obtain the file path:

    Domain name/file path

    For example, if I upload a file named user/woody. PNG and my domain name is cdn.pity. Fun, then the address of my image is:

Uploaded http://cdn.pity.fun/user/woody.png, you can use the test domain name after open the picture give it a try, can also see pictures in file management, will provide the corresponding link.

Pay attention to

The default cache time is very long, which will cause the browser to get the old picture after you overwrite the picture, so we’d better set the corresponding cache time according to the change frequency:

I’ve set it to 5 minutes, which has the advantage of saving money and not having to repeatedly request resources.

Other figure bed

The rest of the charging chart beds are very reliable, mine is mainly aimed at white prostitutes, or is a reliable option for those with a low budget friends.

Today’s content stops here, haven’t written for a long time, a little incoherent ha.