以前的云存储,我都是放在云函数实现的,但是这次新版本之后,云函数调用的方法 data 已经不支持很长的字符串了,我这边需要传 base64 去保存,导致一直报错,所以我这次是换成端侧云存储,具体实现方式如下:
```
jsimport { cloudStorage } from '@kit.CloudFoundationKit';
import common from '@ohos.app.ability.common';
let context = getContext(this) as common.UIAbilityContext;
// 将base64存在沙箱中
async writeFile(data: string): Promise<string> {
let uri = ''
let filesDir = getContext().filesDir;
let filePath = ''
const name = `${Date.now()}.png`
try {
filePath = context.cacheDir + `/${name}`;
uri = fileUri.getUriFromPath(filePath);
let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
const reg = new RegExp("data:image/\w+;base64,")
const base64 = data.replace(reg, "");
const dataBuffer = buffer.from(base64, 'base64')
await fileIo.writeSync(file.fd, dataBuffer.buffer);
fileIo.closeSync(file);
} catch (Error) {
console.log('Error.code', '----->👉', JSON.stringify(Error));
}