我遵循Python教程在端口8000上设置本地dynomodbhttp://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.Python.01.html
from __future__ import print_function # Python 2/3 compatibility
import boto3
dynamodb = boto3.resource('dynamodb', aws_access_key_id="anything", aws_secret_access_key="anything", region_name='us-west-2', endpoint_url="http://localhost:8000")
table = dynamodb.create_table(
TableName='users',
KeySchema=[
{
'AttributeName': 'username',
'KeyType': 'HASH'
},
{
'AttributeName': 'last_name',
'KeyType': 'RANGE'
}
],
AttributeDefinitions=[
{
'AttributeName': 'username',
'AttributeType': 'S'
},
{
'AttributeName': 'last_name',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
print("Table status:", table.table_status)
然而,python运行代码失败,出现以下问题。
不确定是什么原因引起的。
Traceback (most recent call last):
File "C:UsersrbharadwworkspacedynamoDbdynamoDb.py", line 32, in <module>
'WriteCapacityUnits': 5
File "PythonPython35libsite-packagesboto3resourcesfactory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "PythonPython35libsite-packagesboto3resourcesaction.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "PythonPython35libsite-packagesbotocoreclient.py", line 159, in _api_call
return self._make_api_call(operation_name, kwargs)
File "PythonPython35libsite-packagesbotocoreclient.py", line 483, in _make_api_call
operation_model, request_dict)
File "PythonPython35libsite-packagesbotocoreendpoint.py", line 117, in make_request
return self._send_request(request_dict, operation_model)
File "PythonPython35libsite-packagesbotocoreendpoint.py", line 144, in _send_request
request, operation_model, attempts)
File "PythonPython35libsite-packagesbotocoreendpoint.py", line 203, in _get_response
response_dict, operation_model.output_shape)
File "PythonPython35libsite-packagesbotocoreparsers.py", line 211, in parse
parsed = self._do_parse(response, shape)
File "PythonPython35libsite-packagesbotocoreparsers.py", line 587, in _do_parse
parsed = self._parse_shape(shape, original_parsed)
File "PythonPython35libsite-packagesbotocoreparsers.py", line 258, in _parse_shape
return handler(shape, node)
File "PythonPython35libsite-packagesbotocoreparsers.py", line 522, in _handle_structure
raw_value)
File "PythonPython35libsite-packagesbotocoreparsers.py", line 258, in _parse_shape
return handler(shape, node)
File "PythonPython35libsite-packagesbotocoreparsers.py", line 522, in _handle_structure
raw_value)
File "PythonPython35libsite-packagesbotocoreparsers.py", line 258, in _parse_shape
return handler(shape, node)
File "PythonPython35libsite-packagesbotocoreparsers.py", line 522, in _handle_structure
raw_value)
File "PythonPython35libsite-packagesbotocoreparsers.py", line 258, in _parse_shape
return handler(shape, node)
File "PythonPython35libsite-packagesbotocoreparsers.py", line 539, in _handle_timestamp
return self._timestamp_parser(value)
File "PythonPython35libsite-packagesbotocoreutils.py", line 327, in parse_timestamp
return datetime.datetime.fromtimestamp(value, tzlocal())
File "PythonPython35libsite-packagesdateutiltztz.py", line 99, in utcoffset
if self._isdst(dt):
File "PythonPython35libsite-packagesdateutiltztz.py", line 143, in _isdst
return time.localtime(timestamp+time.timezone).tm_isdst
OSError: [Errno 22] Invalid argument
但是,当第二次运行
时,表似乎被创建了botocore.exceptions.ClientError: An error occurred (ResourceInUseException) when calling the CreateTable operation: Cannot create preexisting table
任何建议! !
不幸的是,这个集合是。environ["TZ"] = "UTC"对我不起作用。
所以我遵循一个线程,找到site-packagesdateutiltztz.py文件。在def _naive_is_dst(self, dt)函数中,将其更改为
# workaround the bug of negative offset UTC prob
if timestamp+time.timezone < 0:
current_time = timestamp + time.timezone + 31536000
else:
current_time = timestamp + time.timezone
return time.localtime(current_time).tm_isdst
这是由于boto使用的一个日期处理库存在错误。
当您的时区偏移量为正数时,会发生错误。
您可以通过在导入boto(或它使用的库)之前插入以下代码来解决这个问题。
import os
os.environ["TZ"] = "UTC"
我决定不更改tz.py文件,而是采用Chen Wang的方法,但是用我自己的实现覆盖_naive_is_dst,使tz.py保持不变。
if os.name == 'nt':
def _naive_is_dst(self, dt):
timestamp = tz.tz._datetime_to_timestamp(dt)
# workaround the bug of negative offset UTC prob
if timestamp+time.timezone < 0:
current_time = timestamp + time.timezone + 31536000
else:
current_time = timestamp + time.timezone
return time.localtime(current_time).tm_isdst
tz.tzlocal._naive_is_dst = _naive_is_dst
def _naive_is_dst(self, dt):
timestamp = _datetime_to_timestamp(dt)
if timestamp+time.timezone < 0:
current_time = timestamp + time.timezone + 31536000
else:
current_time = timestamp + time.timezone
return time.localtime(current_time).tm_isdst