Javascript 通过云代码删除解析文件
我在类中有一个作为列的图像。我需要通过查询特定行来删除该图像。但在询问如何删除该图像后 我可以通过以下方式获取图像url:Javascript 通过云代码删除解析文件,javascript,parse-platform,Javascript,Parse Platform,我在类中有一个作为列的图像。我需要通过查询特定行来删除该图像。但在询问如何删除该图像后 我可以通过以下方式获取图像url: var image = results[i].get("imageFile").url(); 我们可以通过获取url来删除图像吗?或者还有其他方法吗?目前只支持通过REST API删除文件。您可以尝试使用Parse.Cloud.httpRequest发出delete file命令 您可以尝试这样做: var image = result.get("imageFile").
var image = results[i].get("imageFile").url();
我们可以通过获取url来删除图像吗?或者还有其他方法吗?目前只支持通过REST API删除文件。您可以尝试使用Parse.Cloud.httpRequest发出delete file命令 您可以尝试这样做:
var image = result.get("imageFile").url();
Parse.Cloud.httpRequest({
method: 'DELETE',
url: image.substring(image.lastIndexOf("/")+1),
headers: {
"X-Parse-Application-Id": "YOUR_APP_ID
"X-Parse-REST-API-Key" : YOUR_API_KEY"
}
);
但别忘了获取URL=只是文件名
这就是为什么你需要这么做
image.substring(image.lastIndexOf("/")+1),
例如,您的URL不应为http://files.parsetfss.com/19728287-9868-4728-8e49-31472daf0211/tfss-65ff02e3-3d11-45a8-ba25-4955f6c7f677-143569529.jpg
但是
tfss-65ff02e3-3d10-45a8-ba25-4955f6c7f677-1435695290。此时仅通过REST API支持删除文件。您可以尝试使用Parse.Cloud.httpRequest发出delete file命令
您可以尝试这样做:
var image = result.get("imageFile").url();
Parse.Cloud.httpRequest({
method: 'DELETE',
url: image.substring(image.lastIndexOf("/")+1),
headers: {
"X-Parse-Application-Id": "YOUR_APP_ID
"X-Parse-REST-API-Key" : YOUR_API_KEY"
}
);
但别忘了获取URL=只是文件名
这就是为什么你需要这么做
image.substring(image.lastIndexOf("/")+1),
例如,您的URL不应为http://files.parsetfss.com/19728287-9868-4728-8e49-31472daf0211/tfss-65ff02e3-3d11-45a8-ba25-4955f6c7f677-143569529.jpg
但是tfss-65ff02e3-3d10-45a8-ba25-4955f6c7f677-1435695290.jpg
几年后,我遇到了这个问题,因为这个SO页面是从ParseServer->S3 Adapter github repo上的一个已解决问题链接而来的
上面的信息和文档中的信息有点令人困惑。文档中写的是URL需要:
https://YOUR.PARSE-SERVER.HERE/parse/files/...profile.png
这是正确的,但仅在您需要提供解析服务器的路径、“文件”目录和文件名本身的意义上。如果有多个应用程序在同一个解析服务器上运行(就像我的情况一样),这就有点棘手了
鉴于Parse自己的服务器上已经不再运行,上述建议(并标记为正确)是错误的或不再准确(四年后)
单独传递文件名将不起作用。您需要传入一个有效的URL-例如
http://localhost:8000/APP-名称/文件/文件名
与文档中不同,您不需要/parse/
。取代/parse/
的是应用程序的名称
最适合我的是:
// note: endpoint is defined outside of this function
// the endpoint is the app/server url - e.g.
// http://localhost:8000/some_parse_app_name
Parse.Cloud.define('deleteFile', (request) => {
let encodedFileName = encodeURI(request.params.fileName)
Parse.Cloud.httpRequest({
headers: {
'X-Parse-Application-Id': appId,
'X-Parse-Master-Key': masterKey
},
method: 'DELETE',
url: `${endpoint}/files/${encodedFileName}`,
}).then(function (httpResponse) {
// success
console.log('Request succeeded: ' + httpResponse.text)
}, function (httpResponse) {
// error
console.error('Request failed with response code ' + httpResponse.status)
})
})
几年后,我偶然发现了这个问题,因为这个SO页面是从ParseServer->S3适配器github repo上的一个已解决问题链接而来的
上面的信息和文档中的信息有点令人困惑。文档中写的是URL需要:
https://YOUR.PARSE-SERVER.HERE/parse/files/...profile.png
这是正确的,但仅在您需要提供解析服务器的路径、“文件”目录和文件名本身的意义上。如果有多个应用程序在同一个解析服务器上运行(就像我的情况一样),这就有点棘手了
鉴于Parse自己的服务器上已经不再运行,上述建议(并标记为正确)是错误的或不再准确(四年后)
单独传递文件名将不起作用。您需要传入一个有效的URL-例如
http://localhost:8000/APP-名称/文件/文件名
与文档中不同,您不需要/parse/
。取代/parse/
的是应用程序的名称
最适合我的是:
// note: endpoint is defined outside of this function
// the endpoint is the app/server url - e.g.
// http://localhost:8000/some_parse_app_name
Parse.Cloud.define('deleteFile', (request) => {
let encodedFileName = encodeURI(request.params.fileName)
Parse.Cloud.httpRequest({
headers: {
'X-Parse-Application-Id': appId,
'X-Parse-Master-Key': masterKey
},
method: 'DELETE',
url: `${endpoint}/files/${encodedFileName}`,
}).then(function (httpResponse) {
// success
console.log('Request succeeded: ' + httpResponse.text)
}, function (httpResponse) {
// error
console.error('Request failed with response code ' + httpResponse.status)
})
})
在解析时删除文件有两个选项。
1.云代码
2.RESTAPI
作为替代方案,您可以在不使用云代码的情况下删除文件。仅使用javascript作为调用api
const remove = async function(URL) {
const httpRequest = await fetch(URL, {
method: 'DELETE',
headers: {
'X-Parse-Application-Id': appId,
'X-Parse-REST-API-Key': resKey,
'X-Parse-Master-Key': masterKey
}
});
const response = await httpRequest;
return response
}
在解析时删除文件有两个选项。
1.云代码
2.RESTAPI
作为替代方案,您可以在不使用云代码的情况下删除文件。仅使用javascript作为调用api
const remove = async function(URL) {
const httpRequest = await fetch(URL, {
method: 'DELETE',
headers: {
'X-Parse-Application-Id': appId,
'X-Parse-REST-API-Key': resKey,
'X-Parse-Master-Key': masterKey
}
});
const response = await httpRequest;
return response
}
是的,谢谢。。。我使用名称“var image=obj.get('imagefile').name();”然后使用“var url=“+image”;这一删除操作真是一团糟,我已经多次询问过他们为什么不想在SDK中实现任何特定的函数,而不是请求REST API。对我来说,它不能与“X-Parse-REST-API-Key”一起工作,用过”而是X-Parse-Master-Key。是的……您知道如何检查文件是否为空。例如,在上述情况下,如何检查“image”是否为空。空表示它有一个与其关联的地址,但该位置没有文件。我不确定是否理解您的问题,但if(result.get(“imageFile”)!==未定义)
如果(result.get(“imageFile”))
也应该这样做。是的,谢谢……我是通过使用“var image=obj.get('imageFile').name()来实现的;然后是“var url=“+image”;这次删除真是一团糟,我已经问过好几次为什么他们不想在SDK中实现任何特定的功能,而不是请求REST API。对我来说,它不能与“X-Parse-REST-API-Key”一起工作,而是使用了“X-Parse-Master-Key”。是的。。。你知道如何检查文件是否为空吗。。例如,在上述情况下,如何检查“图像”是否为空。空表示它有一个与其关联的地址,但该位置没有文件。不确定我是否理解了您的问题,但if(result.get(“imageFile”)!==未定义)
应该执行此操作if(result.get(“imageFile”))
我也在尝试实现类似的功能,但我在服务器的控制台日志中看到了这一点:警告:deleteFile存在重复的云函数。只有最后一个将被使用,其他将被忽略。。。另外,当我尝试调用cloud函数时,我得到了错误:[error]:getaddrinfo ENOTFOUND https(代码:141,版本:1.19.1)。是否由于某种原因无法找到该文件?我不知道发生了什么我正试图实施一些