1

我编写了一个 python 烧瓶 API,它将图像作为输入,将其上传到 S3 存储桶,然后在函数中处理它。当我在本地运行它时,它工作得很好。但是当我通过 Docker 映像运行它时,它会给出botocore.exceptions.NoCredentialsError: Unable to locate credentials.

我的python代码:

import boto3

s3BucketName = "textract_bucket"
region_name = 'ap-southeast-1'

aws_access_key_id = 'advdsav',
aws_secret_access_key = 'sfvsdvsdvsdvvfbdf'

session = boto3.Session(
    aws_access_key_id = aws_access_key_id,
    aws_secret_access_key = aws_secret_access_key,
)

s3 = session.resource('s3')

# Amazon Textract client
textractmodule = boto3.client('textract', region_name = region_name)

def extract_text(doc_name):
    
    response = textractmodule.detect_document_text(
        Document={
            'S3Object': {
                'Bucket': s3BucketName,
                'Name': doc_name,
            }
        })
    
    extracted_items = []
    for item in response["Blocks"]:
        if item["BlockType"] == "LINE":
            extracted_items.append(item["Text"])
            
    return extracted_items

烧瓶 API:

@app.route('/text_extract', methods = ['GET', 'POST'])
def upload_file():
    
   if request.method == 'POST':
       
      img = request.files['file']
      file = secure_filename(img.filename)
      bucket.Object(file).put(Body=img)
      output = extract_text(file)
          
      return {'results': output}

app.run(host="0.0.0.0")

Dockerfile:

FROM python:3.7

RUN apt update 
RUN apt install -y libgl1-mesa-glx

COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN pip install -r requirements.txt

COPY . /app

EXPOSE 5000

ENTRYPOINT [ "python" ]

CMD [ "app.py" ]

我运行的 docker 命令是:

docker build -t text_extract .

接着:docker run -p 5000:5000 text_extract

当我运行 API 并执行发布请求时,我收到了botocore.exceptions.NoCredentialsError错误消息。

我怎样才能解决这个问题?谢谢

4

0 回答 0