Created
February 15, 2024 00:03
-
-
Save DJStompZone/9b82d4a3e4f7fe125f2f35b60be7ac33 to your computer and use it in GitHub Desktop.
Upload a pack to the Bedrock Realms API (NodeJS / Python)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httpx # Install with `pip install httpx` if needed | |
def upload_pack_to_realm(file_path, upload_url, xbl3_token): | |
""" | |
Uploads a pack to a MC Bedrock Realm. | |
Args: | |
file_path (str): The path to the .mcpack file to upload. | |
upload_url (str): The URL to which the pack will be uploaded. | |
xbl3_token (str): The XBL3.0 token for authorization. | |
Returns: | |
dict: The JSON response from the server if successful, else an error message. | |
""" | |
# Request headers | |
headers = { | |
'Content-Type': 'application/octet-stream', | |
'Authorization': f'Bearer {xbl3_token}', | |
} | |
# Open the file in binary mode | |
try: | |
with open(file_path, 'rb') as file: | |
data = file.read() | |
except IOError as e: | |
return f"Error reading the file: {e}" | |
# Send the request and return the response | |
try: | |
response = httpx.post(upload_url, content=data, headers=headers) | |
return response.json() | |
except httpx.HTTPError as err: | |
return f"Error making the request: {err}" | |
# Example usage | |
# if __name__ == "__main__": | |
# file_path = 'path/to/your/pack.mcpack' | |
# upload_url = "https://archive-pocket-production-us-west-2.realms.minecraft.net/packs" | |
# xbl3_token = "XBL3.0 Token here" | |
# | |
# result = upload_pack_to_realm(file_path, upload_url, xbl3_token) | |
# print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const axios = require('axios'); | |
/** | |
* Uploads a pack to a MC Bedrock Realm. | |
* | |
* @param {string} filePath The path to the .mcpack file to upload to the realm. | |
* @param {string} uploadUrl The URL to which the pack will be uploaded. | |
* @param {string} xbl3Token The XBL3.0 token for authorization. | |
* @returns {Promise<Object>} The promise that resolves to the response data from the server. | |
*/ | |
async function uploadPackToRealm(filePath, uploadUrl, xbl3Token) { | |
let packData; | |
// Open the file in binary mode by passing null as the encoding | |
try { | |
packData = fs.readFileSync(filePath, null); | |
} catch (err) { | |
console.error("Error reading the file:", err); | |
process.exit(1); | |
} | |
// Request params | |
const config = { | |
method: 'post', | |
url: uploadUrl, // Obtained via ${realmsapi}/archive/upload/packs/${realmid}/${slot} | |
headers: { | |
'Content-Type': 'application/octet-stream', | |
'Authorization': `Bearer ${xbl3Token}` | |
}, | |
data: packData, | |
maxBodyLength: Infinity, | |
}; | |
// Send the request and return a promose to the result | |
return axios.request(config) | |
.then((response) => response.data) | |
.catch((error) => { | |
console.error(error); | |
throw error; // Rethrow to allow the caller to handle the error (Season to taste) | |
}); | |
} | |
/* | |
====================================== | |
| Example usage | | |
| Note: | | |
| Make sure to handle this in an | | |
| async context, shown below | | |
====================================== | |
async function handleUpload() { | |
const filePath = 'path/to/your/pack.mcpack'; | |
const uploadUrl = "https://archive-pocket-production-us-west-2.realms.minecraft.net/packs"; | |
const xbl3Token = "XBL3.0 Token here"; | |
try { | |
const data = await uploadPackToRealm(filePath, uploadUrl, xbl3Token); | |
console.log("Upload successful:", data); | |
} catch (error) { | |
console.error("Upload failed:", error); | |
} | |
} | |
// Call the async wrapper function to perform the upload | |
handleUpload(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment