云平台不支持写操作,动态生成的文件怎么存储?

如题,比如根据用户信息生成二维码,生成静态文件,再调用。可是云平台不支持写操作,所以图片无法本地保存,也就无法保存,大家有没有解决办法?如果不要求持久化,干脆不生成图片,base64 会报类型错误,TypeError: must be convertible to a buffer, not PilImage,这个要怎么做?
还有如果用云存储,也要生成图片才能上传上去。

阅读 5.4k
3 个回答

感觉这不是通用的思路,云平台大多都有storage 只需要使用这个功能即可

可以生成一个临时文件(系统临时目录,不可能把这个目录也屏蔽了,否则不能做文件上传了),把图片二进制内容写进去。然后再提交这个临时文件,完成后删除即可。例如(PHP举例):

class file {

    private $file = null;

    public function __construct($content=null) {
        $this->file = tempnam(sys_get_temp_dir(),md5(uniqid().mt_rand()));
        if ($content) {
            $this->write($content);
        }
    }

    public function write($content) {
        file_put_contents($this->file,$content);
    }

    public function read($content) {
        return file_get_contents($this->file);
    }

    public function getFilepath() {
        return $this->file;
    }

    public function __destruct() {
        @unlink($this->file);
    }
}
$file = new file();
$file->write(图片二进制);

// cURL上传图片
// CURL_POSTFIELDS : @$file->getFilePath()

python。不晓得

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题