多彩编程 多彩编程MZPH · CODE BLOG
ARTICLE DETAIL

文章详情

深耕前端与后端开发技术的一线实战笔记与踩坑复盘。

如何在项目中集成 erlcloud:rebar3 依赖配置与 Hex 安装终极教程

如何在项目中集成 erlcloud:rebar3 依赖配置与 Hex 安装终极教程 如何在项目中集成 erlcloudrebar3 依赖配置与 Hex 安装终极教程【免费下载链接】erlcloudAWS APIs library for Erlang (Amazon EC2, S3, SQS, DDB, ELB and etc)项目地址: https://gitcode.com/gh_mirrors/er/erlcloud如果您的 Erlang 项目需要对接 AWS 云服务如 EC2、S3、SQS、DynamoDBerlcloud就是最流行的开源选择。它是纯 Erlang 编写的AWS APIs library for Erlang覆盖 Amazon EC2、S3、SQS、DDB、ELB 等数十个服务。本文带来erlcloud rebar3 依赖配置与Hex 安装的终极教程从零开始带您把 erlcloud 集成进自己的项目几分钟内跑通第一个 AWS API 调用。为什么选择 erlcloudAWS 服务支持一览 erlcloud 不是 AWS 官方维护但社区活跃、功能持续更新目前已经实现了绝大多数常用 API计算与存储EC2、ECS、S3、EFS、EBS 相关能力数据库DynamoDBddb2 及流、RDS、Redshift、SimpleDB消息队列SQS、SNS、Kinesis、SES管理与安全IAM、STS、KMS、CloudFormation、CloudTrail、Config更多Athena、Glue、Lambda、Step Functions、WAF、CloudWatch 等erlcloud 支持 OTP 19.3 到 27.1 的多个版本兼容性良好。整体结构清晰核心入口模块包括 erlcloud.erl、erlcloud_ec2.erl、erlcloud_s3.erl配置结构定义在 erlcloud_aws.hrl。方式一rebar3 依赖配置最推荐一键添加 Hex 依赖步骤rebar3 是 Erlang 最主流的构建工具通过 Hex 添加依赖只需两步第 1 步在rebar.config的deps中加入 erlcloud{deps, [ {erlcloud, 3.2.17} ]}.第 2 步执行依赖拉取与编译rebar3 get-deps rebar3 compilerebar3 会自动解析 erlcloud 的传递依赖jsx、lhttpc、eini、base16 等无需手工处理。配置 project_plugins 支持 Hex 发布如果您还需要在项目内管理 Hex 包的发布参考 erlcloud 自身的 rebar.config 配置rebar3_hex插件{project_plugins, [rebar3_hex]}.在 app.src 中声明运行依赖把 erlcloud 加入src/xxx.app.src的applications列表保证应用启动时自动拉起依赖{applications, [kernel, stdlib, crypto, ssl, inets, jsx, lhttpc, eini, base16, erlcloud]}启动时执行application:ensure_all_started(erlcloud).即可完成初始化。方式二通过 git clone 源码集成如果希望直接使用最新源码或定制修改可以克隆仓库后作为本地依赖使用仓库地址为 https://gitcode.com/gh_mirrors/er/erlcloud。git clone https://gitcode.com/gh_mirrors/er/erlcloud cd erlcloud make本地依赖方式在rebar.config中这样引用{deps, [ {erlcloud, {git, 本地绝对路径或子模块路径, {branch, master}}} ]}.快速配置 AWS 凭证的三种方法 方法 1环境变量推荐用于 CI/CDexport AWS_ACCESS_KEY_ID您的 Access Key export AWS_SECRET_ACCESS_KEY您的 Secret Key export AWS_DEFAULT_REGIONus-east-1方法 2使用 ~/.aws 配置文件{ok, Conf} erlcloud_aws:profile(), erlcloud_s3:list_buckets(Conf).该功能由 erlcloud_aws.erl 中的profile/0、profile/1、profile/2函数实现支持命名 profile 与自定义选项。方法 3application 环境变量application:set_env(erlcloud, aws_access_key_id, your key), application:set_env(erlcloud, aws_secret_access_key, your secret key), application:set_env(erlcloud, aws_region, your region).第一个 AWS API 调用实战演练使用进程字典配置最简单erlcloud_s3:configure(AccessKeyId, SecretAccessKey), erlcloud_s3:list_buckets().使用配置对象适合多账户场景Conf erlcloud_ec2:new(AccessKeyId, SecretAccessKey), erlcloud_ec2:describe_images(Conf).配置对象#aws_config{}记录定义在 erlcloud_aws.hrl包含各服务的 host、端口、协议等默认值您可以按需修改字段后传入任意 API。常见问题排查FAQQ1编译报错缺少依赖检查rebar3 get-deps是否成功以及网络是否能访问 Hex 仓库。Q2提示 region 不存在确认AWS_DEFAULT_REGION已正确设置或通过erlcloud_ec2:configure(AK, SK, ec2.eu-west-1.amazonaws.com)指定主机。Q3如何切换多账户使用配置对象方式为每个账户创建独立的#aws_config{}调用时作为最后一个参数传入即可适合跨 AWS 账户访问场景。结语通过本文的erlcloud rebar3 依赖配置与Hex 安装教程您已经可以快速在 Erlang 项目中集成 erlcloud 并调用 AWS API。建议优先采用 Hex rebar3 方式管理依赖生产环境注意使用 IAM 临时凭证与最小权限策略。动手试试第一个erlcloud_s3:list_buckets()吧【免费下载链接】erlcloudAWS APIs library for Erlang (Amazon EC2, S3, SQS, DDB, ELB and etc)项目地址: https://gitcode.com/gh_mirrors/er/erlcloud创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表