You need to enable JavaScript to run this app.
文档中心
火山引擎入门实验

火山引擎入门实验

复制全文
下载 pdf
存储
SDK 通过代理访问 TOS
复制全文
下载 pdf
SDK 通过代理访问 TOS

本文介绍 SDK 通过代理访问 TOS 的过程。

前言

当客户端位于火山网络外,且大部分客户端无法访问外网,如客户端位于客户本地机房且无外网访问权限时,可以通过搭建代理服务器,将SDK的请求通过代理发送到TOS服务端。

TOS SDK 支持设置 HTTP 协议代理服务发送请求(目前只支持 HTTP 协议代理)。

本实验使用Go SDK跟squid代理服务实现代理转发SDK请求。

关于实验
  • 预计部署时间:30分钟
  • 级别:初级
  • 相关产品:TOS
  • 受众: 通用
实验说明
  • 点击此链接登录控制台。

  • 如果您还没有账户,请点击此链接注册账户。

实验步骤

一、安装squid

您可以使用如下命令安装squid并启动服务。

yum install squid -y
systemctl start squid
systemctl status squid

安装成功后,进程状态如下:

[root@xxxxxx ~]# systemctl status squid
● squid.service - Squid caching proxy
   Loaded: loaded (/usr/lib/systemd/system/squid.service; disabled; vendor preset: disabled)
   Active: active (running) since 四 2023-10-19 21:22:18 CST; 5 days ago
  Process: 12335 ExecStart=/usr/sbin/squid $SQUID_OPTS -f $SQUID_CONF (code=exited, status=0/SUCCESS)
  Process: 12329 ExecStartPre=/usr/libexec/squid/cache_swap.sh (code=exited, status=0/SUCCESS)
 Main PID: 12338 (squid)
    Tasks: 3
   Memory: 14.0M
   CGroup: /system.slice/squid.service
           ├─12338 /usr/sbin/squid -f /etc/squid/squid.conf
           ├─12340 (squid-1) -f /etc/squid/squid.conf
           └─12341 (logfile-daemon) /var/log/squid/access.log

配置文件如下:

[root@xxxx ~]# cat /etc/squid/squid.conf
#
# Recommended minimum configuration:
#

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl localnet src 10.0.0.0/8	# RFC1918 possible internal network
acl localnet src 172.16.0.0/12	# RFC1918 possible internal network
acl localnet src 192.168.0.0/16	# RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines

acl SSL_ports port 443
acl Safe_ports port 80		# http
acl Safe_ports port 21		# ftp
acl Safe_ports port 443		# https
acl Safe_ports port 70		# gopher
acl Safe_ports port 210		# wais
acl Safe_ports port 1025-65535	# unregistered ports
acl Safe_ports port 280		# http-mgmt
acl Safe_ports port 488		# gss-http
acl Safe_ports port 591		# filemaker
acl Safe_ports port 777		# multiling http
acl CONNECT method CONNECT

#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
#http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports
#http_access deny CONNECT !SSL_ports
http_access allow localnet
# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny manager

# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
http_access allow localnet
http_access allow localhost
# And finally deny all other access to this proxy
#http_access deny all
http_access allow all
# Squid normally listens to port 3128
http_port 3128

# Uncomment and adjust the following to add a disk cache directory.
#cache_dir ufs /var/spool/squid 100 16 256

# Leave coredumps in the first cache dir
coredump_dir /var/spool/squid

#
# Add any of your own refresh_pattern entries above these.
#
refresh_pattern ^ftp:		1440	20%	10080
refresh_pattern ^gopher:	1440	0%	1440
refresh_pattern -i (/cgi-bin/|\?) 0	0%	0
refresh_pattern .		0	20%	4320

本配置文件只做示例演示,您可以根据实际环境调整配置文件。

二、代码实现

本示例代码使用TOS Go SDK完成,示例为上传对象至TOS。

示例代码如下:

package main

import (
	"fmt"
	"github.com/volcengine/ve-tos-golang-sdk/v2/tos"
	"golang.org/x/net/context"
	"os"
)

func checkErr(err error) {
	if err != nil {
		if serverErr, ok := err.(*tos.TosServerError); ok {
			fmt.Println("Error:", serverErr.Error())
			fmt.Println("Request ID:", serverErr.RequestID)
			fmt.Println("Response Status Code:", serverErr.StatusCode)
			fmt.Println("Response Header:", serverErr.Header)
			fmt.Println("Response Err Code:", serverErr.Code)
			fmt.Println("Response Err Msg:", serverErr.Message)
		} else if clientErr, ok := err.(*tos.TosClientError); ok {
			fmt.Println("Error:", clientErr.Error())
			fmt.Println("Client Cause Err:", clientErr.Cause.Error())
		} else {
			fmt.Println("Error:", err)
		}
		panic(err)
	}
}

func main() {
	var (
		ak       = "xxx"
		sk       = "xxx"
		endpoint = "http://tos-cn-beijing.volces.com"
		region   = "cn-beijing"

		proxyHost = "http://代理服务器地址"
		proxyPort = 3128 //代理服务器端口
	)
	proxy, err := tos.NewProxy(proxyHost, proxyPort)
	checkErr(err)
	credential := tos.NewStaticCredentials(ak, sk)
	// 可以通过 tos.WithProxy 的方式添加请求代理
	client, err := tos.NewClientV2(endpoint, tos.WithCredentials(credential), tos.WithRegion(region), tos.WithProxy(proxy))
	checkErr(err)
	fmt.Println(client.Client)
	fmt.Println("start")


	file, _ := os.Open("/Users/xxxxx")

	inputStruct := tos.PutObjectBasicInput{
		Bucket: "xxxx",
		Key:    "xxxx",
	}

	putResult, err1 := client.PutObjectV2(context.Background(), &tos.PutObjectV2Input{
		inputStruct,
		file,
	})

	checkErr(err1)

	fmt.Println(putResult)

	client.Close()
}

程序执行后,您可以看到proxy返回的响应,然后可以通过控制台检查上传对象是否成功。

参考文档:
[1] TOS Go SDK

最近更新时间:2024.01.02 20:41:14
这个页面对您有帮助吗?
有用
有用
无用
无用