一、注册创建云存储
1.注册地址
https://console.upyun.com/register/?invite=XsDm-Glu5
2.注册后白嫖地址
https://www.upyun.com/?utm_source=lianmeng&utm_medium=referral
没有人会拒绝白嫖吧
白嫖需要的页尾代码
本网站由 <a href="https://www.upyun.com/?utm_source=lianmeng&utm_medium=referral" target="_blank"><img src="https://www.seclusion.work/%E5%8F%88%E6%8B%8Dlogo/%E5%8F%88%E6%8B%8D%E4%BA%91_logo5.png" width="60px" height="30px"> </a>提供 CDN 加速 / 云存储 服务
3.创建云储存
在顶部:云产品 -> 云存储
注意保存自己的密码,只显示一次
a.绑定域名
绑定的需已备案的域名
b.去域名商添加解析
绑定后会有cname值直接复制的,如上图
c.上传图片
二、去服务器写两个文件
在你的网站根目录选一个地方创建 autoserve
目录
目录里面创建 一个 autoapi.py
一个 autoapi.php
就py文件的 4-9 行需要自己填值,其它基本不用改
#这个是 py 文件
import upyun
import os
service = 'xxx' #本文图三中的服务名称
username = 'xxx' #本文图三中的操作员名称
password = 'xxxxxxxxxxxxx' #本文图三中的操作员密码
path1 = '/xxxxx/' #填写你随机图片又拍云对象存储里的路径
url = "https://xxx.xxxxxxxxx.com" #填写你的cdn域名
file_path1 = "/www/wwwroot/网站根目录/autoserve/img.txt" #填写你img.txt 在服务器里面的路径
up = upyun.UpYun(service, username, password, timeout=30, endpoint=upyun.ED_AUTO) #初始化
res = up.getlist(path1) #读取该目录下的图片列表
with open(file_path1,'w') as f:
for i in res:
f.write(url + path1 + i["name"]+'\n')
print("链接写入成功")
<?php
/**********************************************
* 文件名 : autoapi.php
* 这个是PHP文件
***********************************************/
if($_GET['folder']){
$folder=$_GET['folder'];
}else{
$folder='nothumb';
}
//存有图片链接的文件名img.txt
$filename = "img.txt";
if(!file_exists($filename)){
die('文件不存在');
}
//从文本获取链接
$pics = [];
$fs = fopen($filename, "r");
while(!feof($fs)){
$line=trim(fgets($fs));
if($line!=''){
array_push($pics, $line);
}
}
//从数组随机获取链接
$pic = $pics[array_rand($pics)];
//返回指定格式
$type=$_GET['type'];
switch($type){
//JSON返回
case 'json':
header('Content-type:text/json');
die(json_encode(['pic'=>$pic]));
default:
die(header("Location: $pic"));
}
?>
* * *
三、跑py文件生成img.txt
----------------
### 1.在服务器命令符安装python3
### 2.运行命令安装两个py依赖
pip3 install requests
pip3 install upyun
```
### 3.运行py文件
还是在命令符里
```
python3 /www/wwwroot/网站根目录/autoserve/autoapi.py
```
检查一下生成的 img.txt 文件有没有链接,有就没问题
没有内容检查一下各个路径是否正确,一般是路径问题
四、测试
----
到这步就当你以上皆正确
在浏览器访问
```
https://你的网站/autoserve/autoapi.php
```
看看是否返回图片
* * *
五、感谢看完
------
> 实在报错无法解决
>
> 可以通过评论
>
> 或者博客首页图中心的社交方式联系我
>
> 尽我所能
>
> 阿巴阿巴阿巴阿巴阿巴阿巴阿巴阿巴阿巴阿巴阿巴阿巴阿巴阿巴