gpt4 book ai didi

python - python中该公式的具体正则表达式

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:44 34 4
gpt4 key购买 nike

我正在尝试创建一个正则表达式来匹配我的 s3 存储桶中的此文件。这是我尝试创建的 S3 key ,但现在显然无法正确找到它。下面我提供了我尝试使用此正则表达式方程访问的路径/文件夹的存储桶的快照。

这又是针对 s3 key 的,下面我还将发布我正在使用的代码部分。我引用的是 try 语句内部。

class process_raw_snowplow_event_data(luigi.Task):
dataset_date = luigi.DateParameter(default=date.today() - timedelta(days=1))
# force_run = luigi.BoolParameter()
_start = luigi.DateSecondParameter(default=datetime.utcnow())
file_root = luigi.Parameter()


def download_s3_file(self, s3_filename):

local_filename = "/Users/xxx/etl/%s" % s3_filename

s3_file_full_path =re.compile("snowplow-enrich-output/enriched/archive/run=" + self.dataset_date.strftime("%Y-%m-%d") +r"-\d{2}-\d{2}-\d{2}/*.")


try:
s3.download_file(Bucket=os.environ.get('SP_BUCKET'), Key=s3_file_full_path, Filename=local_filename)
except Exception as e:
logger.error("%s - Could not retrieve %s because: %s" % ("download_s3_file()", s3_filename, e))
raise

错误:

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/luigi/worker.py", line 199, in run
new_deps = self._run_get_new_deps()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/luigi/worker.py", line 139, in _run_get_new_deps
task_gen = self.task.run()
File "target.py", line 123, in run
infile_name = self.download_s3_file(s3_filename)
File "target.py", line 47, in download_s3_file
s3.download_file(Bucket=os.environ.get('SP_BUCKET'), Key=s3_filename, Filename=local_filename)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/boto3/s3/inject.py", line 172, in download_file
extra_args=ExtraArgs, callback=Callback)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/boto3/s3/transfer.py", line 307, in download_file
future.result()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/s3transfer/futures.py", line 73, in result
return self._coordinator.result()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/s3transfer/futures.py", line 233, in result
raise self._exception
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/s3transfer/tasks.py", line 255, in _main
self._submit(transfer_future=transfer_future, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/s3transfer/download.py", line 353, in _submit
**transfer_future.meta.call_args.extra_args
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/botocore/client.py", line 314, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/botocore/client.py", line 612, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (404) when calling the HeadObject operation: Not Found

谢谢大家

enter image description here

最佳答案

S3.Client.download_file 不支持作为正则表达式的 Key 参数。 1

s3_file_full_path 计算结果为类似于以下对象的对象

re.compile(r'snowplow-enrich-output/enriched/archive/run=2019-02-24-\d{2}-\d{2}-\d{2}/*.',
re.UNICODE)

您的存储桶快照中没有任何对象是这样命名的。

解决此问题的唯一方法是列出存储桶对象并将其键与上述正则表达式对象进行匹配。 2

上面的正则表达式类似于列出名称前缀为 'snowplow-enrich-output/enriched/archive/run=2019-02-24-' 的对象

list_object_kwargs = {
'Bucket': os.environ.get('SP_BUCKET'),
'Prefix': 'snowplow-enrich-output/enriched/archive/run=2019-02-24-'
}

def object_keys(contents):
return [content['Key'] for content in contents]

objects = s3.list_objects_v2(**list_object_kwargs)
found_object_keys = object_keys(objects['Contents'])

while objects['IsTruncated']:
objects = s3.list_objects_v2(
ContinuationToken=objects['ContinuationToken'],
**list_object_kwargs
)
found_object_keys.extend(object_keys(objects['Contents']))

try:
for key in found_object_keys:
s3.download_file(
Bucket=os.environ.get('SP_BUCKET'),
Key=key)
except Exception as e:
logger.error(
"Could not retrieve %s because: %s" % (key, e))
raise

关于python - python中该公式的具体正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54486229/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com