Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase未使用Javascript将正确的图像上载到存储器_Javascript_Firebase_Google Cloud Functions_Google Cloud Storage_Firebase Storage - Fatal编程技术网

Firebase未使用Javascript将正确的图像上载到存储器

Firebase未使用Javascript将正确的图像上载到存储器,javascript,firebase,google-cloud-functions,google-cloud-storage,firebase-storage,Javascript,Firebase,Google Cloud Functions,Google Cloud Storage,Firebase Storage,我正在从事一个使用Firebase实时数据库和Firebase存储的项目 第一部分: 我有一个用javascript上传用户图片的功能: 常量firebaseConfig={ apiKey:FB_WEB_API_KEY, authDomain:FB_WEB_AUTH_DOMAIN, 数据库URL:FB\U WEB\U数据库\U URL, projectId:FB_WEB_项目ID, storageBucket:FB_WEB_STORAGE_BUCKET, appId:FB_WEB_APP_ID

我正在从事一个使用Firebase实时数据库和Firebase存储的项目

第一部分: 我有一个用javascript上传用户图片的功能:


常量firebaseConfig={
apiKey:FB_WEB_API_KEY,
authDomain:FB_WEB_AUTH_DOMAIN,
数据库URL:FB\U WEB\U数据库\U URL,
projectId:FB_WEB_项目ID,
storageBucket:FB_WEB_STORAGE_BUCKET,
appId:FB_WEB_APP_ID,
}
firebase=require('firebase/app')
require('firebase/functions')
需要('firebase/数据库')
需要('firebase/storage')
需要('firebase/auth')
require('firebase/functions')
firebase.initializeApp(firebaseConfig)
导出异步函数setUserPicture(pId、用户ID、图片){
如果(图片!=''&&picture!=null){
让承诺=[]
const filesRef=firebase.storage().ref(`users/${pId}/${userId}`)
filesRef.listAll().then(异步文件夹=>{
for(让文件夹.items的文件){
promises.push(filesRef.child(file.name).delete())
}
//等待删除所有以前的图像
等待承诺。所有(承诺)
让attachmentRef=firebase
.储存
.ref(`users/${pId}/${userId}/${userId}@${Date.now()}`)
attachmentRef.put(图片)。然后(=>{
attachmentRef.getDownloadURL()。然后(url=>{
火基
.数据库()
.ref(`users/${pId}/${userId}`)
.update({photoURL:url})
})
})
})
}
}
在此功能中,所有与用户头像相关的图像都将按照以下模式上传到Firebase Storage中名为users的文件夹中:

'users/${pId}/${userId}/'//此路径内放置图像
在上载新图像之前,我会从存储中的特定文件夹中删除所有以前的图像,以确保没有以前的图像

同样,当我上传图像时,我获取
downloadURL
,并将其保存在用户数据字段中

这是为此安装的软件包:

"firebase": "^7.12.0",
"firebase-admin": "^8.11.0",
"firebase-functions": "^3.6.1",
第二部分: 对于这个项目,我有一些云函数来处理一些数据。 在这种情况下,我有一个云功能,将2个调整大小的图像保存在存储器中,保存在我拥有化身原始图像的相同文件夹中。这是为了使用合适的尺寸来缩短加载时间

const functions=require('firebase-functions'))
const admin=require('firebase-admin')
const{tmpdir}=require('os')
常量{dirname,join}=require('path')
常量夏普=需要(“夏普”)
常量fs=require('fs-extra')
常量{uuid}=require('uuidv4')
exports.onUploadUserPicture=函数
.runWith({内存:'2GB',timeoutSeconds:120})
.storage.object()
.onFinalize(异步对象=>{
const bucket=admin.storage().bucket()
const originalPath=object.name
const originalFileName=originalPath.split('/').pop()
const userId=originalFileName.split('@').shift()
const bucketDir=dirname(原始路径)
const workingDir=join(tmpdir(),“调整大小”)
const tmpPath=join(workingDir,'origin.png')
常量元数据={
contentDisposition:object.contentDisposition,
contentEncoding:object.contentEncoding,
contentLanguage:object.contentLanguage,
contentType:object.contentType,
元数据:object.metadata | |{},
}
metadata.metadata.resizedImage=true
log(`Apply resizing to${originalFileName}`)
如果(!originalPath.includes('users')| |!object.contentType.includes('image')){
log(`此文件不是用户图片。退出!`)
返回错误
}else if(原始文件名,包括“(U 50x50”)){
console.log(`ready processed:[${originalFileName}],大小:[50]。退出!`)
返回错误
}else if(原始文件名,包括(“U 300x300”)){
console.log(`ready processed:[${originalFileName}],大小:[300]。退出!`)
返回错误
}
等待fs.ensureDir(工作目录)
wait bucket.file(originalPath).download({destination:tmpPath})
常数大小=[50300]
const promises=size.map(异步大小=>{
log(`SINGING${originalFileName}的大小为${size}`)
const newImgName=`${userId}@${Date.now()}{size}x${size}`
const imgPath=join(workingDir,newImgName)
等待夏普(tmpPath)
.clone()
.resize(大小,大小,{fit:'inside',withoutenlarge:true})
.toFile(imgPath)
log(`Just resized${newImgName}的大小为${size}`)
//如果原始映像具有下载令牌,请添加
//要调整大小的图像的新标记
if(object.metadata.firebaseStorageDownloadTokens){
metadata.metadata.firebaseStorageDownloadTokens=uuid()
}
返回bucket.upload(imgPath{
目的地:加入(bucketDir,newImgName),
元数据:元数据,
})
})
等待承诺。所有(承诺)
返回fs.remove(workingDir)
})
在这个云函数中,首先,我验证它是否是用户图片(基于原始图像的位置)以及它是否不是大小调整后的图像(没有_50x50或_300x300)

然后,我下载原始图像,创建调整大小的缩略图,然后上传到存储器

我从原始图像中应用了一些元数据,约定名称是唯一的,并且还为每个图像生成了一个新的存储下载令牌

问题 真正的问题是,当我在Firebase存储中验证时