在 RoR 中创建 CKAN 资源,以字符串形式提供文件内容



如何在RoR中创建CKAN资源,提供包含文件内容的字符串?

这似乎是一个有效的命令行解决方案(如果我将文件保存到文件系统(:

curl -H 'Authorization: <api_key>' 'https://demo.ckan.org/api/action/resource_create' --form upload=@file.csv --form package_id=<pck_id>

鉴于我有字符串形式的CSV文件内容,如何将其上传到CKAN站点?

这是我现在拥有的代码,资源已创建,但其内容显示为空白。

http_client = HTTPClient.new
temp_file = Tempfile.open('csv_export_tmp_file')
temp_file.write(resource_content)
body = {
name: <filename>,
title: <filetitle>,
package_id: <package_id_here>,
description: <description>,
created: <created_at_time>,
upload: temp_file,
mimetype: 'text/csv',
resource_type: 'file',
format: 'csv'
}
response = http_client.post(resource_create_url, body, [['Authorization', api_key], ['Content-Type', 'multipart/form-data']])
temp_file.close
temp_file.unlink

这是我最终使用的解决方案

resource_create_url = 'ckan_site_url.com/api/action/resource_create'
http_client = HTTPClient.new
extended_string_io = Class.new(StringIO) do
def path
'data.csv'
end
end
virtual_file = extended_string_io.new("my, csv, file, contentn1,2,3,4")
body = {
name: <filename>,
title: <filetitle>,
package_id: <package_id_here>,
description: <description>,
created: <created_at_time>,
upload: virtual_file,
resource_type: 'file',
format: 'csv'
}
response = http_client.post(resource_create_url, body, [['Authorization', api_key]])

最新更新