在 Remotion Lambda 中使用代理
#在 Remotion Lambda 中使用代理v4.0.315
Remotion Lambda 支持通过接受一个 requestHandler 选项来使用 HTTP/HTTPS 代理进行所有 AWS API 调用,该选项允许你传递自定义的 AWS SDK 请求处理程序。
🌐 Remotion Lambda supports using HTTP/HTTPS proxies for all AWS API calls by accepting a requestHandler option that allows you to pass a custom AWS SDK request handler.
当你的环境要求所有外部 HTTP 请求都必须通过代理服务器时,这非常有用。
🌐 This is useful when your environment requires all external HTTP requests to go through a proxy server.
设置代理
🌐 Setting up a proxy
1. 安装代理代理程序
🌐 1. Install a proxy agent
首先,安装一个 HTTP/HTTPS 代理代理包,例如 https-proxy-agent:
🌐 First, install an HTTP/HTTPS proxy agent package like https-proxy-agent:
npm install https-proxy-agent2. 创建一个带有代理的请求处理器
🌐 2. Create a request handler with proxy
创建一个使用你的代理的请求处理程序:
🌐 Create a request handler that uses your proxy:
import {HttpsProxyAgent } from 'https-proxy-agent';
// Configure your proxy URL
const proxyUrl = 'http://your-proxy-server:8080';
// Create a proxy agent
const proxyAgent = new HttpsProxyAgent (proxyUrl );
// Create a request handler that uses the proxy
export const proxyRequestHandler = {
httpsAgent : proxyAgent ,
};3. 使用请求处理器与 Remotion Lambda 函数
🌐 3. Use the request handler with Remotion Lambda functions
将 requestHandler 选项传递给任意 Remotion Lambda 函数:
🌐 Pass the requestHandler option to any Remotion Lambda function:
import {getFunctions } from '@remotion/lambda/client';
import {proxyRequestHandler } from './proxy-setup';
const functions = await getFunctions ({
region : 'us-east-1',
compatibleOnly : true,
requestHandler : proxyRequestHandler ,
});
console .log ('Functions:', functions );支持的功能
🌐 Supported functions
Lambda 客户端中的所有公共 AWS 相关 API 都接受 requestHandler 选项。
🌐 All public AWS-related APIs in the Lambda client accept the requestHandler option.
带身份验证的示例
🌐 Example with authentication
如果你的代理需要身份验证,你可以在代理 URL 中包含凭据:
🌐 If your proxy requires authentication, you can include credentials in the proxy URL:
import {HttpsProxyAgent } from 'https-proxy-agent';
// Proxy with authentication
const proxyUrl = 'http://username:password@your-proxy-server:8080';
const proxyAgent = new HttpsProxyAgent (proxyUrl );
export const authenticatedProxyRequestHandler = {
httpsAgent : proxyAgent ,
};TypeScript 支持
🌐 TypeScript support
Remotion Lambda 导出一个 RequestHandler 类型,你可以使用它来保证类型安全:
🌐 Remotion Lambda exports a RequestHandler type that you can use for type safety:
import type {RequestHandler } from '@remotion/lambda/client';
import {HttpsProxyAgent } from 'https-proxy-agent';
const proxyAgent = new HttpsProxyAgent ('http://proxy:8080');
const myRequestHandler : RequestHandler = {
httpsAgent : proxyAgent ,
};特定环境配置
🌐 Environment-specific configuration
你可以根据你的环境有条件地使用代理:
🌐 You can conditionally use a proxy based on your environment:
import {HttpsProxyAgent } from 'https-proxy-agent';
import type {RequestHandler } from '@remotion/lambda/client';
const createRequestHandler = (): RequestHandler | undefined => {
const proxyUrl = process .env .HTTPS_PROXY || process .env .HTTP_PROXY ;
if (proxyUrl ) {
return {
httpsAgent : new HttpsProxyAgent (proxyUrl ),
};
}
// Return undefined to use default behavior
return undefined ;
};
export const requestHandler = createRequestHandler ();然后在你的 Lambda 调用中使用它:
🌐 Then use it in your Lambda calls:
import {getFunctions } from '@remotion/lambda/client';
import {requestHandler } from './conditional-proxy';
const functions = await getFunctions ({
region : 'us-east-1',
compatibleOnly : true,
requestHandler , // This will be undefined if no proxy is configured
});另请参阅
🌐 See also