学习bug
2025.8.6,在发帖功能中,一直出现上传的图片页面无法上传的问题,bucket中有上传的图片,但是数据库中的image_url一直为空,复制up的源码也没有用
记录一下我的解决办法
export const uploadFile = async (image_key: string, file: ImageResult) => {
try {
const res = await storage.createFile('689318ae002bdaf1ece1', image_key, {
name: image_key,
type: 'image/jpeg',
size: file.height * file.width,
uri: file.uri,
});
const fileId = res.$id;
// 手动拼接图片 URL
const endpoint = 'https://cloud.appwrite.io/v1';
const projectId = 'XXX';
const bucketId = 'XXX';
const fileUrl = `${endpoint}/storage/buckets/${bucketId}/files/${fileId}/view?project=${projectId}&mode=admin`;
return {
fileId,
fileUrl,
};
} catch (error) {
console.log('uploadFile error:', error);
throw error;
}
};
const pickImage = async () => {
try {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ["images", "videos"],
allowsEditing: true,
aspect: [4, 3],
quality: 1,
});
if (!result.canceled) {
const compressedImage = await compressImage(result.assets[0].uri);
if (compressedImage) {
if (!compressedImage) {
Alert.alert("图片压缩失败");
return;
}
const { fileId, fileUrl } = await uploadFile(
ID.unique(),
compressedImage
);
if (!fileUrl) {
Alert.alert("图片上传失败");
return;
}
setImage(fileUrl);
}
}
} catch (error) {
console.log(error);
Alert.alert("图片选择失败");
}
};
希望能帮助后来的朋友😘👌