【问题标题】:Supabase not allowing upload of files into storage bucketSupabase 不允许将文件上传到存储桶中
【发布时间】:2022-12-16 02:19:17
【问题描述】:

我有一个简单的脚本,用于将 .txt 文件上传到名为“training-data”的 Supabase 存储桶中。存储桶已公开,我确保包含必要的存储/对象策略以允许访问。我知道其他人和我一样遇到了以下错误,但我设法使用策略修复了它:

'new row violates row-level security policy for table "objects"'

我现在的问题是,它给了我一个不同的错误,据我所知,以前没有人遇到过:

storage3.utils.StorageException: {'statusCode': 400, 'error': 'Key is not present in table "buckets".', 'message': 'insert or update on table "objects" violates foreign key constraint "objects_bucketId_fkey"'}

我不确定为什么它告诉我我的存储桶是一个表并且它需要一个唯一的标识符键。我该如何解决?我的代码如下:

from supabase import create_client
from dotenv import load_dotenv
import os

load_dotenv()
url = os.environ.get("DEPLOY_URL")
key = os.environ.get("DEPLOY_KEY")


supabase = create_client(url, key)
data = supabase.table("Blog Data").select("*").execute()

file = "dzonescrape\\dzonescrape\\spiders\\test.txt"
# data = supabase.storage().from_("public/training-data").download("test.txt")
# print(data)

supabase.storage().from_("public/training-data").upload("test.txt", file)

仅供参考,我测试了下载手动上传的文件,并且它有效,所以只是上传给我带来了问题。这是我的插入对象策略:

CREATE POLICY "Make inserting training data publicly available (INSERT)" ON "storage"."objects"
AS PERMISSIVE FOR INSERT
TO public

WITH CHECK (bucket_id = 'training-data')

【问题讨论】:

  • 存储桶中是否已有 test.txt 文件?
  • 我确保在尝试插入时将其删除。
  • 您是否尝试过将 upload() 与更新插入集一起使用?例如.from_("training-data").upload('test.txt', file, file_options={"x-upsert": "true"})
  • 是的,我有,但它仍然给我同样的错误。

标签: python database supabase supabase-database


【解决方案1】:

尝试更改您的存储桶名称:

supabase.storage().from_("training-data").upload("test.txt", file)

我在 Dart 上检查了我的代码:

  Future<StorageResponse> _uploadFile() async {
    final supabase = Supabase.instance.client;

    const file = '/Users/igdmitrov/DEV/Test.txt';
    final response = await supabase.storage
        .from('training-data')
        .upload('text.txt', File(file));

    print(response.error);

    return response;
  }

它适用于政策:

CREATE POLICY "Give users authenticated access to folder 1ov7tml_0" ON storage.objects FOR INSERT TO public WITH CHECK (bucket_id = 'training-data');

CREATE POLICY "Give users authenticated access to folder 1ov7tml_1" ON storage.objects FOR SELECT TO public USING (bucket_id = 'training-data');

【讨论】:

    【解决方案2】:

    我遇到了这个问题并以这种方式解决了它。

    在 storage.objects > New Policy 下转到 Storage > Policies > Other policies

    然后从截图上传policy,就可以上传了enter image description here

    【讨论】:

      猜你喜欢
      • 2019-09-28
      • 2022-01-08
      • 2020-04-02
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 2018-03-25
      相关资源
      最近更新 更多