import os
import requests
url = "https://s3.amazonaws.com/my-bucket/data/file.txt"
auth_header = os.environ.get("AWS_AUTH_HEADER")
headers = {
"Authorization": auth_header,
"Content-Type": "text/plain"
}
body = "Updated content"
response = requests.put(url, headers=headers, data=body)
if response.status_code >= 400:
print(response.text)
else:
print("Success")
import os
import httpx
url = "https://s3.amazonaws.com/my-bucket/data/file.txt"
auth_header = os.environ.get("AWS_AUTH_HEADER")
headers = {
"Authorization": auth_header,
"Content-Type": "text/plain"
}
body = "Updated content"
with httpx.Client() as client:
response = client.put(url, headers=headers, content=body)
if response.status_code >= 400:
print(response.text)
else:
print("Success")
const fetch = require('node-fetch');
const url = 'https://s3.amazonaws.com/my-bucket/data/file.txt';
const authHeader = process.env.AWS_AUTH_HEADER;
const headers = {
'Authorization': authHeader,
'Content-Type': 'text/plain'
};
const body = 'Updated content';
fetch(url, {
method: 'PUT',
headers: headers,
body: body
})
.then(response => {
if (!response.ok) {
return response.text().then(text => { throw new Error(text); });
}
return response.text();
})
.then(() => console.log('Success'))
.catch(err => console.error(err.message));
const axios = require('axios');
const url = 'https://s3.amazonaws.com/my-bucket/data/file.txt';
const authHeader = process.env.AWS_AUTH_HEADER;
const headers = {
'Authorization': authHeader,
'Content-Type': 'text/plain'
};
const body = 'Updated content';
axios.put(url, body, { headers })
.then(response => {
console.log('Success');
})
.catch(error => {
if (error.response) {
console.error(error.response.data);
} else {
console.error(error.message);
}
});
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func main() {
url := "https://s3.amazonaws.com/my-bucket/data/file.txt"
authHeader := os.Getenv("AWS_AUTH_HEADER")
body := "Updated content"
req, err := http.NewRequest("PUT", url, io.NopCloser(strings.NewReader(body)))
if err != nil {
fmt.Println("Error creating request:", err)
return
}
req.Header.Set("Authorization", authHeader)
req.Header.Set("Content-Type", "text/plain")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
return
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
} else {
fmt.Println("Success")
}
}
<?php
$url = 'https://s3.amazonaws.com/my-bucket/data/file.txt';
$authHeader = getenv('AWS_AUTH_HEADER');
$headers = [
'Authorization: ' . $authHeader,
'Content-Type: text/plain'
];
$body = 'Updated content';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode >= 400) {
echo $response;
} else {
echo "Success";
}
require 'net/http'
require 'uri'
url = URI.parse('https://s3.amazonaws.com/my-bucket/data/file.txt')
auth_header = ENV['AWS_AUTH_HEADER']
headers = {
'Authorization' => auth_header,
'Content-Type' => 'text/plain'
}
body = 'Updated content'
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Put.new(url.request_uri, headers)
request.body = body
response = http.request(request)
if response.code.to_i >= 400
puts response.body
else
puts 'Success'
end
import Foundation
let url = URL(string: "https://s3.amazonaws.com/my-bucket/data/file.txt")!
var request = URLRequest(url: url)
request.httpMethod = "PUT"
request.setValue(ProcessInfo.processInfo.environment["AWS_AUTH_HEADER"], forHTTPHeaderField: "Authorization")
request.setValue("text/plain", forHTTPHeaderField: "Content-Type")
request.httpBody = "Updated content".data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print(error.localizedDescription)
return
}
guard let httpResponse = response as? HTTPURLResponse else {
print("Invalid response")
return
}
if httpResponse.statusCode >= 400 {
if let data = data, let responseBody = String(data: data, encoding: .utf8) {
print(responseBody)
}
} else {
print("Success")
}
}
task.resume()
RunLoop.main.run()
use std::env;
use reqwest;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let url = "https://s3.amazonaws.com/my-bucket/data/file.txt";
let auth_header = env::var("AWS_AUTH_HEADER")?;
let body = "Updated content";
let client = reqwest::Client::new();
let response = client
.put(url)
.header("Authorization", auth_header)
.header("Content-Type", "text/plain")
.body(body)
.send()
.await?;
if !response.status().is_success() {
let body = response.text().await?;
println!("{}", body);
} else {
println!("Success");
}
Ok(())
}
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
var url = "https://s3.amazonaws.com/my-bucket/data/file.txt";
var authHeader = Environment.GetEnvironmentVariable("AWS_AUTH_HEADER");
var body = "Updated content";
using var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Put, url);
request.Headers.Add("Authorization", authHeader);
request.Headers.Add("Content-Type", "text/plain");
request.Content = new StringContent(body);
var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine("Success");
}
}
}