Boto3 put_object标记中支持非ASCII字符

问题描述

我们正在使用 boto3 文件上传 AWS S3 。我们需要在文件添加标签,并且在上传文件时已成功将标签添加到每个文件中。

问题在于,当字符串包含非ASCII字符时, boto3 会为无效值引发异常:

ñÑóú

但是S3在其Tagging中支持Non-ASCII字符,因为我已经成功地手动设置了它们。 我们如何在Boto3的 put_object 方法标记中启用对UTF-8字符的支持

这是我们的代码

form = await request.form()
contents = await form["upload_file"].read()
s3 = boto3.client('s3',aws_access_key_id=C.ACCESS_KEY_S3,aws_secret_access_key=C.SECRET_KEY_S3)
response = s3.put_object(
            Body=contents,Bucket=C.S3_BUCKET,Key='{id}/{filename}'.format(id=deviceid,filename=filename),ACL='public-read' if public else "private",Tagging=tagging if tagging is not None else "" #typical: "title=Título&comments=Ññ"
            )

这是一个例外:

 File "/usr/local/lib/python3.7/dist-packages/botocore/client.py",line 635,in _make_api_call
raise error_class(parsed_response,operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidTag) when calling the PutObject operation: The TagValue you have provided is invalid

解决方法

根据S3 documentation

testWidgets("Should show loading button when click on request button ",(WidgetTester tester) async {
      await tester.pumpWidget(
        _buildWidget(
          home: FinancialDialogBill(
            paymentId: "fakePaymentId",additionalValueType: AdditionalValueType.fee,billValues: billValues,installmentList: installments,),);

      expect(find.byKey(financialDialogBillRequestButton),findsOneWidget);
      expect(find.byKey(financialDialogBillRequestButtonLoading),findsNothing);

      await tester.tap(find.byKey(financialDialogBillRequestButton));

      await tester.pump();

      expect(
          find.byKey(financialDialogBillRequestButtonLoading),findsOneWidget);
      expect(find.byKey(financialDialogBillRequestButton),findsNothing);

    });
  });

仅在基于浏览器的上载和SOAP上允许使用非ASCII字符。

根据official boto3 git page,您可以使用boto上传非ASCII元数据,但是由于boto使用REST,并且在REST中仅允许使用US-ASCII,因此不能保证(由于更新)。