简体   繁体   English

尝试复制到 python shell 中的 AWS Glue tmp 文件夹时出错

[英]Error When trying to copy to AWS Glue tmp folder in python shell

I'm trying to copy some files over to the tmp folder using boto3 in a glue job.我正在尝试在胶水作业中使用 boto3 将一些文件复制到tmp文件夹。 Here's my code:这是我的代码:

import pandas as pd
import numpy as np
import boto3

bucketname = "<bucket_name>"
s3 = boto3.resource('s3')
my_bucket = s3.Bucket(bucketname)
print('line 9')
source = "stuff/20210223/"
#target = temp directory job is running in
target = os.path.dirname(os.path.realpath(__file__))

for obj in my_bucket.objects.filter(Prefix=source):
    print('line 15')
    source_filename = (obj.key).split('/')[-1]
    copy_source = {
        'Bucket': bucketname,
        'Key': obj.key
    }
    print(obj.key)
    print('line 21')
    target_filename = "/{}/{}".format(target, source_filename)
    print('target_filename')
    print(target_filename)
    s3.meta.client.copy(copy_source, bucketname, target_filename)
    print('line 27')


print('curr dir')
curr_dir = os.path.dirname(os.path.realpath(__file__))
print('\n----------------\n')
dir_path = os.path.dirname(os.path.realpath(__file__))
files = [f for f in os.listdir('.') if os.path.isfile(f)]
print(files)

This yields the following error:这会产生以下错误:

  File "/tmp/runscript.py", line 123, in <module>
    runpy.run_path(temp_file_path, run_name='__main__')
  File "/usr/local/lib/python3.6/runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/usr/local/lib/python3.6/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/local/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/tmp/glue-python-scripts-hcnfmxbn/CDF parser.py", line 26, in <module>
  File "/usr/local/lib/python3.6/site-packages/boto3/s3/inject.py", line 379, in copy
    return future.result()
  File "/usr/local/lib/python3.6/site-packages/s3transfer/futures.py", line 106, in result
    return self._coordinator.result()
  File "/usr/local/lib/python3.6/site-packages/s3transfer/futures.py", line 265, in result
    raise self._exception
  File "/usr/local/lib/python3.6/site-packages/s3transfer/tasks.py", line 255, in _main
    self._submit(transfer_future=transfer_future, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/s3transfer/copies.py", line 110, in _submit
    **head_object_request)
  File "/usr/local/lib/python3.6/site-packages/botocore/client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python3.6/site-packages/botocore/client.py", line 661, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/tmp/runscript.py", line 142, in <module>
    raise e_type(e_value).with_traceback(new_stack)
TypeError: __init__() missing 1 required positional argument: 'operation_name'

Have followed just about every SO thread about this error message but solutions seem to have to do with region, and I already checked that the region of my glue job is the same as my bucket.几乎每个 SO 线程都关注此错误消息,但解决方案似乎与区域有关,我已经检查过胶水作业的区域与我的存储桶相同。 The error occurs in this line of code: s3.meta.client.copy(copy_source, bucketname, target_filename)错误出现在这行代码: s3.meta.client.copy(copy_source, bucketname, target_filename)
When I actually try to copy the file over to the tmp folder.当我实际尝试将文件复制到 tmp 文件夹时。 I don't understand how this can be a permissions issue associated with my write access to the folder with my Glue service IAM role, because I can save csv's to the folder using pandas to_csv我不明白这怎么可能是与我使用 Glue 服务 IAM 角色对文件夹的写访问权限相关的权限问题,因为我可以使用 pandas to_csv将 csv 保存到文件夹中

Solved by updating the IAM Role policy I was using for the glue job to allow write access to the bucket the job is running in.通过更新我用于胶水作业的 IAM 角色策略以允许对正在运行作业的存储桶进行写访问来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM