2016-08-12 108 views
3

我跟進了dynamodb Python的教程上的8000端口 http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.Python.01.htmlDynamodb CREATE_TABLE呼叫失敗

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) 

然而蟒蛇運行的代碼與下面的問題沒有設置當地dynomodb。

不知道是什麼導致它。

Traceback (most recent call last): 
File "C:\Users\rbharadw\workspace\dynamoDb\dynamoDb.py", line 32, in <module> 
'WriteCapacityUnits': 5 
File "Python\Python35\lib\site-packages\boto3\resources\factory.py", line 520, in do_action 
response = action(self, *args, **kwargs) 
File "Python\Python35\lib\site-packages\boto3\resources\action.py", line 83, in __call__ 
response = getattr(parent.meta.client, operation_name)(**params) 
File "Python\Python35\lib\site-packages\botocore\client.py", line 159, in _api_call 
return self._make_api_call(operation_name, kwargs) 
File "Python\Python35\lib\site-packages\botocore\client.py", line 483, in _make_api_call 
operation_model, request_dict) 
File "Python\Python35\lib\site-packages\botocore\endpoint.py", line 117, in make_request 
return self._send_request(request_dict, operation_model) 
File "Python\Python35\lib\site-packages\botocore\endpoint.py", line 144, in _send_request 
request, operation_model, attempts) 
File "Python\Python35\lib\site-packages\botocore\endpoint.py", line 203, in _get_response 
response_dict, operation_model.output_shape) 
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 211, in parse 
parsed = self._do_parse(response, shape) 
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 587, in _do_parse 
parsed = self._parse_shape(shape, original_parsed) 
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 258, in _parse_shape 
return handler(shape, node) 
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 522, in _handle_structure 
raw_value) 
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 258, in _parse_shape 
return handler(shape, node) 
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 522, in _handle_structure 
raw_value) 
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 258, in _parse_shape 
return handler(shape, node) 
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 522, in _handle_structure 
raw_value) 
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 258, in _parse_shape 
return handler(shape, node) 
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 539, in _handle_timestamp 
return self._timestamp_parser(value) 
File "Python\Python35\lib\site-packages\botocore\utils.py", line 327, in parse_timestamp 
return datetime.datetime.fromtimestamp(value, tzlocal()) 
File "Python\Python35\lib\site-packages\dateutil\tz\tz.py", line 99, in utcoffset 
    if self._isdst(dt): 
File "Python\Python35\lib\site-packages\dateutil\tz\tz.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 

任何建議!

+0

看起來你已經有了一個名爲「users」的表。 – garnaat

+0

是的,第二次運行是不能創建預先存在的表的錯誤。 但是,我在第一次運行時遇到了特定於時間的錯誤 –

+1

這看起來像是「datetutils」,Windows平臺和可能的32位版本的Python之間的問題。這個線程是相關的(https://github.com/dateutil/dateutil/issues/197),但似乎沒有提供一個簡單的答案,但可能會有所幫助。 – garnaat

回答

2

這是由於boto使用的日期處理庫之一的錯誤。

當你的時區偏移量是一個正數時就會出現這個錯誤。

您可以通過在導入boto(或其使用的庫)之前插入以下代碼來解決該問題。

import os 
os.environ["TZ"] = "UTC" 
+1

這不適用於我(Windows 10 64位,Python 3.5.1,而是我做了一個變通,如果'timestamp + time.timezone <0'我添加31536000到它(區別於1.1。1970年和1.1。1971 in seconds)。我希望它不會造成任何錯誤... – NikoNyrh

+0

你是英雄,你救了我的痛苦和淚水 –

3

不幸的是,設置os.environ [「TZ」] =「UTC」不適用於我。

因此,我遵循一個線程,找到site-packages \ dateutil \ tz \ tz.py文件。在DEF _naive_is_dst(個體經營,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 
-3
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 
0

而是改變tz.py文件,我決定把陳望的做法,但我自己的實現覆蓋_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