aws的上传、删除s3文件以及图像识别文字功能

准备工作

安装aws cli

根据自己的操作系统,下载相应的安装包安装。安装过程很简单,在此不再赘述。

在安装完成之后,运行以下两个命令来验证AWS CLI是否安装成功。参考以下示例,在MacOS上打开Terminal程序。如果是Windows系统,打开cmd。

  • where aws / which aws 查看AWS CLI安装路径
  • aws --version 查看AWS CLI版本
zonghan@MacBook-Pro ~ % aws --version
aws-cli/2.0.30 Python/3.7.4 Darwin/21.6.0 botocore/2.0.0dev34
zonghan@MacBook-Pro ~ % which aws
/usr/local/bin/aws

初始化配置AWS CLI

在使用AWS CLI前,可使用aws configure命令,完成初始化配置。

zonghan@MacBook-Pro ~ % aws configure
AWS Access Key ID [None]: AKIA3GRZL6WIQEXAMPLE
AWS Secret Access Key [None]: k+ci5r+hAcM3x61w1example
Default region name [None]: ap-east-1
Default output format [None]: json
  • AWS Access Key ID 及AWS Secret Access Key可在AWS管理控制台获取,AWS CLI将会使用此信息作为用户名、密码连接AWS服务。

    点击AWS管理控制台右上角的用户名 --> 选择Security Credentials

  • 点击Create New Access Key以创建一对Access Key ID 及Secret Access Key,并保存(且仅能在创建时保存)

  • Default region name,用以指定要连接的AWS 区域代码。每个AWS区域对应的代码可通过 此链接查找。
  • Default output format,用以指定命令行输出内容的格式,默认使用JSON作为所有输出的格式。也可以使用以下任一格式:

    JSON(JavaScript Object Notation)

    YAML: 仅在 AWS CLI v2 版本中可用

    Text

    Table

更多详细的配置请看该文章

s3存储桶开通

该电脑配置的认证用户在aws的s3上有权限访问一个s3的存储桶,这个一般都是管理员给你开通

图像识别文字功能开通

该电脑配置的认证用户在aws的Amazon Textract的权限,这个一般都是管理员给你开通

aws的sdk

import boto3
from botocore.exceptions import ClientError, BotoCoreError

安装上述boto3的模块,一般会同时安装botocore模块

上传文件

方法一

使用upload_file方法来上传文件

import logging
import boto3
from botocore.exceptions import ClientError
import os def upload_file(file_path, bucket, file_name=None):
"""Upload a file to an S3 bucket :param file_name: File to upload
:param bucket: Bucket to upload to
:param object_name: S3 object name. If not specified then file_name is used
:return: True if file was uploaded, else False
""" # If S3 object_name was not specified, use file_name
if object_name is None:
object_name = os.path.basename(file_name) # Upload the file
s3_client = boto3.client('s3')
# s3 = boto3.resource('s3')
try:
response = s3_client.upload_file(file_path, bucket, file_name)
# response = s3.Bucket(bucket).upload_file(file_name, object_name)
except ClientError as e:
logging.error(e)
return False
return True

方法二

使用PutObject来上传文件

import logging
import os
import boto3
from botocore.exceptions import ClientError, BotoCoreError
from django.conf import settings
from celery import shared_task logger = logging.getLogger(__name__) def upload_file_to_aws(file_path, bucket, file_name=None):
"""Upload a file to an S3 bucket
:param file_path: File to upload
:param file_name: S3 object name. If not specified then file_path is used
:return: True if file was uploaded, else False
""" # If S3 object_name was not specified, use file_name
if file_name is None:
file_name = os.path.basename(file_path) # Upload the file
s3 = boto3.resource('s3')
try:
with open(file_path, 'rb') as f:
data = f.read()
obj = s3.Object(bucket, file_name)
obj.put(
Body=data
)
except BotoCoreError as e:
logger.info(e)
return False
return True

删除文件

def delete_aws_file(file_name, bucket):
try:
s3_client = boto3.client("s3")
s3_client.delete_object(Bucket=bucket, Key=file_name)
except Exception as e:
logger.info(e)

图像识别文字

识别发票、账单这种key,value的形式

def get_labels_and_values(result, field):
if "LabelDetection" in field:
key = field.get("LabelDetection")["Text"]
value = field.get("ValueDetection")["Text"]
if key and value:
if key.endswith(":"):
key = key[:-1]
result.append({key: value}) def process_text_detection(bucket, document):
try:
client = boto3.client("textract", region_name="ap-south-1")
response = client.analyze_expense(
Document={"S3Object": {"Bucket": bucket, "Name": document}}
)
except Exception as e:
logger.info(e)
raise "An unknown error occurred on the aws service"
result = {}
for expense_doc in response["ExpenseDocuments"]:
for line_item_group in expense_doc["LineItemGroups"]:
for line_items in line_item_group["LineItems"]:
for expense_fields in line_items["LineItemExpenseFields"]:
get_labels_and_values(result, expense_fields)
for summary_field in expense_doc["SummaryFields"]:
get_labels_and_values(result, summary_field)
return result def get_extract_info(bucket, document):
return process_text_detection(bucket, document)

单纯的识别文字

#Analyzes text in a document stored in an S3 bucket. Display polygon box around text and angled text
import boto3
import io
from io import BytesIO
import sys import math
from PIL import Image, ImageDraw, ImageFont def ShowBoundingBox(draw,box,width,height,boxColor): left = width * box['Left']
top = height * box['Top']
draw.rectangle([left,top, left + (width * box['Width']), top +(height * box['Height'])],outline=boxColor) def ShowSelectedElement(draw,box,width,height,boxColor): left = width * box['Left']
top = height * box['Top']
draw.rectangle([left,top, left + (width * box['Width']), top +(height * box['Height'])],fill=boxColor) # Displays information about a block returned by text detection and text analysis
def DisplayBlockInformation(block):
print('Id: {}'.format(block['Id']))
if 'Text' in block:
print(' Detected: ' + block['Text'])
print(' Type: ' + block['BlockType']) if 'Confidence' in block:
print(' Confidence: ' + "{:.2f}".format(block['Confidence']) + "%") if block['BlockType'] == 'CELL':
print(" Cell information")
print(" Column:" + str(block['ColumnIndex']))
print(" Row:" + str(block['RowIndex']))
print(" Column Span:" + str(block['ColumnSpan']))
print(" RowSpan:" + str(block['ColumnSpan'])) if 'Relationships' in block:
print(' Relationships: {}'.format(block['Relationships']))
print(' Geometry: ')
print(' Bounding Box: {}'.format(block['Geometry']['BoundingBox']))
print(' Polygon: {}'.format(block['Geometry']['Polygon'])) if block['BlockType'] == "KEY_VALUE_SET":
print (' Entity Type: ' + block['EntityTypes'][0]) if block['BlockType'] == 'SELECTION_ELEMENT':
print(' Selection element detected: ', end='') if block['SelectionStatus'] =='SELECTED':
print('Selected')
else:
print('Not selected') if 'Page' in block:
print('Page: ' + block['Page'])
print() def process_text_analysis(bucket, document): #Get the document from S3
s3_connection = boto3.resource('s3') s3_object = s3_connection.Object(bucket,document)
s3_response = s3_object.get() stream = io.BytesIO(s3_response['Body'].read())
image=Image.open(stream) # Analyze the document
client = boto3.client('textract') image_binary = stream.getvalue()
response = client.analyze_document(Document={'Bytes': image_binary},
FeatureTypes=["TABLES", "FORMS"]) ### Alternatively, process using S3 object ###
#response = client.analyze_document(
# Document={'S3Object': {'Bucket': bucket, 'Name': document}},
# FeatureTypes=["TABLES", "FORMS"]) ### To use a local file ###
# with open("pathToFile", 'rb') as img_file:
### To display image using PIL ###
# image = Image.open()
### Read bytes ###
# img_bytes = img_file.read()
# response = client.analyze_document(Document={'Bytes': img_bytes}, FeatureTypes=["TABLES", "FORMS"]) #Get the text blocks
blocks=response['Blocks']
width, height =image.size
draw = ImageDraw.Draw(image)
print ('Detected Document Text') # Create image showing bounding box/polygon the detected lines/text
for block in blocks: DisplayBlockInformation(block) draw=ImageDraw.Draw(image)
if block['BlockType'] == "KEY_VALUE_SET":
if block['EntityTypes'][0] == "KEY":
ShowBoundingBox(draw, block['Geometry']['BoundingBox'],width,height,'red')
else:
ShowBoundingBox(draw, block['Geometry']['BoundingBox'],width,height,'green') if block['BlockType'] == 'TABLE':
ShowBoundingBox(draw, block['Geometry']['BoundingBox'],width,height, 'blue') if block['BlockType'] == 'CELL':
ShowBoundingBox(draw, block['Geometry']['BoundingBox'],width,height, 'yellow')
if block['BlockType'] == 'SELECTION_ELEMENT':
if block['SelectionStatus'] =='SELECTED':
ShowSelectedElement(draw, block['Geometry']['BoundingBox'],width,height, 'blue') #uncomment to draw polygon for all Blocks
#points=[]
#for polygon in block['Geometry']['Polygon']:
# points.append((width * polygon['X'], height * polygon['Y']))
#draw.polygon((points), outline='blue') # Display the image
image.show()
return len(blocks) def main(): bucket = ''
document = ''
block_count=process_text_analysis(bucket,document)
print("Blocks detected: " + str(block_count)) if __name__ == "__main__":
main()

aws上传文件、删除文件、图像识别的更多相关文章

  1. github 上传或删除 文件 命令

    git clone https://github.com/onionhacker/bananaproxy.git cd ~/../.. git config --global user.email & ...

  2. java 通过sftp服务器上传下载删除文件

    最近做了一个sftp服务器文件下载的功能,mark一下: 首先是一个SftpClientUtil 类,封装了对sftp服务器文件上传.下载.删除的方法 import java.io.File; imp ...

  3. 通过代码链接ftp上传下载删除文件

    因为我的项目是Maven项目,首先要导入一个Maven库里的包:pom.xml <dependency>            <groupId>com.jcraft</ ...

  4. 七牛云-上传、删除文件,工具类(Day49)

    要求: 1. java1.8以上 2. Maven: 这里的version指定了一个版本范围,每次更新pom.xml的时候会尝试去下载7.5.x版本中的最新版本,你可以手动指定一个固定的版本. < ...

  5. github上传和删除文件(三)

    上传文件: git init git add * git commit -m "description" //git remote rm origin 或查看当前 git remo ...

  6. java FTP 上传下载删除文件

    在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...

  7. 使用eclipse-hadoop插件无法再eclipse操作(上传、删除文件)

    再conf中的hdfs-site.xml添加如下配置: <property><name>dfs.permissions</name><value>fal ...

  8. FastDfs java客户端上传、删除文件

    #配置文件 connect_timeout = 2 network_timeout = 30 charset = UTF-8 http.tracker_http_port = 9090 http.an ...

  9. Struts2 文件上传,下载,删除

    本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...

随机推荐

  1. 说起分布式自增ID只知道UUID?SnowFlake(雪花)算法了解一下(Python3.0实现)

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_155 但凡说起分布式系统,我们肯定会对一些海量级的业务进行分拆,比如:用户表,订单表.因为数据量巨大一张表完全无法支撑,就会对其进 ...

  2. 关于Tornado5.1:到底是真实的异步和还是虚假的异步

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_107 我们知道Tornado 优秀的大并发处理能力得益于它的 web server 从底层开始就自己实现了一整套基于 epoll ...

  3. 承上启下继往开来,Python3上下文管理器(ContextManagers)与With关键字的迷思

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_217 在开发过程中,我们会经常面临的一个常见问题是如何正确管理外部资源,比如数据库.锁或者网络连接.稍不留意,程序将永久保留这些资 ...

  4. C# 虚方法、抽象方法

    一.虚方法(virtual) 作用:当有一个定义在类中的函数需要在继承类中实现时,可以使用虚方法. 示例: class Person { public virtual void XXX() { Con ...

  5. eplan中数据库运行提速

    access,sql,是指部件库的存储方式,eplan支持两种方式即Microsoft Office access,Microsoft SQL Server,可以通过这两种方式打开部件库,如果要打开数 ...

  6. MySQL表操作过程的基础代码解析

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. MySQL 的表有很多种,对表的操作主要是增删改查,今天来浅谈一下这些操作的底层代码和流程,以下以 tmp table为例 ...

  7. vue自定义switch开关,使用less支持换肤

    实际项目用到了,记录一下,也方便以后使用,这样也可以避免为了使用一个switch,引入整个外部web框架: 也可以方便更好的理解是和使用less. 基础代码使用的是网上的,然后自己添加了less换肤, ...

  8. 使用SSH连接解决git报错:fatal: unable to access 'https://github.com/xxx/xxx.github.io.git/': Proxy CONNECT aborted

    TL;DRs 这个错误的原因和HTTPS的代理配置有关,使用SSH方式连接可以避免这一问题 最近git pull和push的时候总是报错 fatal: unable to access 'https: ...

  9. CSS 笔记目录

    布局 CSS 布局(一):Flex 布局 选择器 CSS 选择器(一):属性选择器 CSS 选择器(二):子代选择器(>)

  10. Seatunnel超高性能分布式数据集成平台使用体会

    @ 目录 概述 定义 使用场景 特点 工作流程 连接器 转换 为何选择SeaTunnel 安装 下载 配置文件 部署模式 入门示例 启动脚本 配置文件使用参数示例 Kafka进Kafka出的ETL示例 ...